When I run the following in cmd prompt, it works fine
pushd "C:\Converter"
for /f "delims=" %g in ('dir "C:\Toconvert" /b /s /ad') do (powershell .\BatchConvert.ps1 "%g" 10)
popd
It executes the command successfully.
This is the output:
C:\Converter>(powershell .\BatchConvert.ps1 "C:\Toconvert\Prego!PDF" 10 )
When I tried running the same command in a .bat file, it fails. Using cmd line to run the bat file I find this is what it is generating:
C:\Converter>for /f "delims=" \Toconvert" /b /s /ad') do (powershell .\BatchConvert.ps1 "g" 10)
Why is it dropping off the "%g in ('dir "C:" before the brackets and why is it dropping off the % in the do command? How do I escape characters to get this to work as a .bat?
% must be doubled as %% when used in batch files – dbenham May 17 '13 at 12:39
Related
I need a batch script to list .avi|.mp4 and run a command for all directories that contain such files:
mencoder.exe <some_arguments> -o "output/(name_of_directory).mp4" <list_of_files_in_directory_spearated_by_spaces_evey_file_quoted>
Could anybody help me with that? Is it possible at all?
I made a script in PHP, compiled it with BamCompile, but it seems to act totally different on Windows 8, arrays are being converted to strings and something really weird is goin on...
try this:
#echo off &setlocal
cd /d "%userprofile%\Videos"
for /f "tokens=1*delims=:" %%a in ('dir /b /a-d *.avi *.mp4^|findstr /n $') do set "$%%a=%%~b"
setlocal enabledelayedexpansion
for /f "tokens=1*delims==" %%a in ('set "$" 2^>nul') do set "line=!line! "%%~b""
echo mencoder.exe [some_arguments] -o "output/(name_of_directory).mp4" %line%
Look at the output and remove echo if it looks good.
See if this floats your boat. ! characters are verboten in paths and filenames.
#echo off
setlocal enabledelayedexpansion
cd /d "%userprofile%\Videos"
for /f "delims=" %%a in ('dir /s /b /a-d *.avi *.mp4') do (
for /f "delims=" %%b in ("%%~dpa.") do (
set "line="
for %%c in ("%%~dpa*.*") do set line=!line! "%%c"
echo mencoder.exe [some_arguments] -o "output\%%~nxb.mp4" !line!
pause
)
)
I have the next script, and I need it to save all the xcopy files copy outputs to one log file,
:tmdeploy
title Deploying Edithor - %deployer%
set src_folder=S:\ApliTelinver\Compilacion\Edithor 10.5\CompilacionQA
set dst_folder=S:\ApliTelinver\Ambientes-Edithor\Sincronizacion\Test\Test-Mantenimiento
set filelist=filelist-tm.txt
echo Origen: %src_folder%
echo Destino: %dst_folder%
echo.
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
)
echo.
pause
goto end
The problem is that I only get the last file copy in the output. And how to properly do a timestamp for .log file?
Thank you
You should use the appended redirection operator, >> instead of >.
So, your for loop will look like this:
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
)
I have a windows batch file that does this:
for %%s in (*.sql) do call
It loops through all the sql script in a folder.
In the folder the file names are like:
s4.06.01.sql
s4.07.01.sql
s4.08.01.sql
s4.10.01.sql
s5.01.sql
But the for loop goes through the files randomly (not in the name order), it first run s5.01, then s4.06, then s4.08, then s4.10, then s4.07. How can I make them run in the name order?
It used to work, but now it doesn't. What may cause this problem?
Jerry's answer might very well be what is causing the problem.
You might solve it by changing
for %%s in (*.sql) do call
to
for %%s in (dir"*.sql ^| sort) do call
Edit cudo's to LonelyPixel
The posted solution does not work as is on a Windows 8 machine (perhaps it did back then with Windows XP but I have no idea).
Following is a working solution using Windows 8 command prompt.
for /f "tokens=*" %%s in ('dir /b *.sql ^| sort') do echo %%s
If memory serves, it will operate on the files in the order they're returned by the file system. As such, if you run it on a disk partition that's formatted with NTFS, the names will be returned in sorted order so they'll be processed in sorted order. If the disk partition is formatted with something like FAT or FAT32, the names will be retrieved (and processed) in a more or less random order.
I too had to deal with spaces in the filenames, so I added quotes around the input variable like so:
for /f "tokens=*" %%s in ('dir /b *.sql ^| sort') do echo "%%s"
In my case it was SQLCMD, with the scripts in a subdirectory, like so:
SET "machineName=localhost"
for /f "tokens=*" %%f in ('dir /b PostDeployScripts\*.sql ^| sort') do sqlcmd -E -S%machineName% -i"PostDeployScripts\%%f"
Question: I've several text files containing sql create table/column/view/storedProcedure textfiles. Now I want to merge the textfiles into one textfile.
I go into the directory, and type:
type *.sql >> allcommands.sql
Now to problem is I should add the text ' GO ' after every file's content.
I can append Go by doing
type *.sql >> allcommands.sql & echo GO >> allcommands.sql
But this only inserts go once.
How can I accomplish this with DOS commands ?
You want something like this:
for %%f in (*.sql) do type %%f >>allcommands.sql & echo GO >> allcommands.sql
The %% is for use in a batch file. If you're not running it from a batch file you only need single % signs.
Try this one
for %%f in (*.sql) do (
type %%f >>allcommands.sql
echo. >> allcommands.sql
echo GO >> allcommands.sql
echo. >> allcommands.sql )
it adds newline then go for each SQL file. it works for me try it.
Use copy to concatenate the first file with a file with "GO" text, then concatenate again with the second file.
#echo off
CLS
::concat.bat outfile.sql
setlocal EnableDelayedExpansion
If EXIST GOTMP.TMP DEL /Q GOTMP.TMP
Echo GO>GOTMP.TMP
ECHO.>>GOTMP.TMP
If EXIST "%~1" DEL /Q "%~1"
Echo.>"%~1"
for /f "tokens=*" %%A in ('dir /a-d /on /b "*.sql"') do call :perfaction "%%A%" "%~1"
ECHO Done Generating Output "%~1"
ECHO.
pause
Goto :EOF
:perfaction
ECHO "%~1"
copy "%~2"+"%~1"+GOTMP.TMP "%~2"
GOTO :EOF
for /f %%i in ('someprogram %1 2>&1 | find /c "some string"') do ...
it says
2>&1 was unexpected at this time
You will need to escape special characters within the for command:
for /f %%i in ('someprogram %1 2^>^&1 ^| find /c "some string"') do ...
cmd's parser is not the most robust one; this is unfortunately necessary.