Command runs in commandline but not in a BAT file - windows

I have a one liner batch command that sorts files by date and then deletes everything but the last 10. This command runs just fine when I run it in a CMD window. However, when I place it in a BAT file, I get errors.
Command (works OK in CMD window):
for /f "skip=10 delims=" %A in ('dir /a:-d /b /o:-d /t:c *.jpg ^2^>nul') do del %A
Errors I get if trying to run it in a batch file:
Q:\Testbk>test1
-d was unexpected at this time.
Q:\Testbk>for /f "skip=10 delims=" -d /b /o:-d /t:c *.jpg ^2^>nul") do del A
Any idea as to how to fix it to run in a BAT file would be very much appreciated.

You need %%A in the batch file. I changed your original batch-file code to type rather than delete
for /f "skip=4 delims=" %%A in ('dir /a:-d /b /o:-d /t:c *.jpg 2^>nul') do type "%%A"
because I didn't want to delete my files.

Related

Retrieve name of latest txt file in Command Prompt

I am trying to retrieve the name of the newest .txt file in a directory, on Windows 6.1 , C:\Users\KMST
I have tried the following but it does not seem to work.
for /f "delims=" %%x in ('C:\Users\KMST\*.txt /od /b *.*') do #echo %%x
But the error I got is, %%x was unexpected at this time.
From command line the syntax for for command does not need to escape the percent sign used in the replaceable parameter, so
for /f "delims=" %x in ('dir /a-d /b /o-d C:\Users\KMST\*.txt') do #echo %x
but this will list all the files in descending date order.
To only get the newest file from command line you can try with
dir /a-d /o-d /b "C:\Users\KMST\*.txt" | cmd /v /c"set /p.=&&echo(^!.^!"

Batch file / add file names to command

Looking for a way to run a command, but insert the path/filenames of all mp4 files from the working directory (where the script is run) to the command.
vlc.exe c:\path\filename1.mp4 c:\path\filename2.mp4
I have the following code, but the "%%~A" is only inserting one path/filename at a time instead of adding every path filename from the folder.
set dir=C:\Users\Administrator\Desktop\1
for /f "delims=" %%A in ('dir /b "%dir%\*.*"') do ("C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "%%~A" --sout=#transcode{vcodec=mp2v,vb=800,acodec=mpga,ab=128,channels=2,samplerate=44100}:http{mux=ts,dst=:8080/abc} --sout-keep --loop)
An elementary solution: first try
set "dir=C:\Users\Administrator\Desktop\1"
dir /b /a:-d "%dir%\*.*"
The same result (filenames only) as in:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%%~A"
So add full path as follows:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%dir%\%%~A"
Eventually, replace #echo "%dir%\%%~A" with your command...

windows 7 bat file leaves out parts of the command

I made a batch file with one command in it so that i dont have to remember the code. thought it would be simple however the code that it prints out when running it is not the code that is in the file. heres the only line in the file
for /f "tokens=*" %f in ('dir /a:-D /s /b') do move "%f" .
I have run this code in the command prompt and it does what i want it to however when i put it in a batch file and run it this the code that is read by the cmd
C:\Users\Erik\Desktop\google music backup>movefromsubfolders
-D was unexpected at this time.
C:\Users\Erik\Desktop\google music backup>for /f "tokens=*" -D /s /b') do move "f" .
why is it screwing everything up? This is the first bat file i have ever attempted to write so i could possibly be doing something stupid
you need double % when used in batch:
for /f "tokens=*" %%f in ('dir /a:-D /s /b') do move "%%f"

How can I Delete Files That Do Not Contain Certain Characters Using Windows Batch File?

How can I delete files that do not end with the pattern .PDF_*.pdf ?
AA00A6E2.PDF
AA00A6E3.PDF
AA00A6E3.PDF_01.pdf
AA00A6E3.PDF_02.pdf
AA00A6E3.PDF_03.pdf
I have been trying all sorts of variations around the following syntax:
FOR %%F IN (%varFolderSource%\*.*) DO IF NOT "%%~xF" == "*_*" DEL /F /S "%%F"
But I can't seem to crack it.
%varFolderSource% is a folder path: C:\Temp etc.
I am running this in a Windows 7 batch file.
For /f "delims=" %A in ('dir /b /a-d ^|findstr /i /v /e /r "\.PDF_[a-z0-9]*\.pdf"') do echo %A
Type
for /?
findstr /?
In a batch file use %%A rather than %A at command prompt.

How to execute any .exe by using a batch file?

I know that I can start an exe by doing:
start "" /b filename.exe
But that requires me to know the name of filename.exe, how could I do that for any general file ending with .exe? I tried the obvious wildcard implementation:
start "" /b *.exe
Windows, however, gives me an error saying it cannot find "*.exe" file.
if you plan to run inside a batch file you can do in this way:
for %%i in (*.exe) do start "" /b "%%i"
if you want to skip a particular file to be executed:
for %%i in (*.exe) do if not "%%~nxi" == "blabla.exe" start "" /b "%%i"
if is necessary to check also the subfolders add the /r parameter:
for /r %%i in (*.exe) do start "" /b "%%i"
From cmd run this to the folder that has all the exe you wish to run:
for %x in (*.exe) do ( start "" /b "%x" )
Hoep it helps
for /f "delims=" %%a in ('dir /b /s "*.exe"') do (
start %%a
)
You should first use dir command to find all exe files, and then execute it.
In a bat file add this line
FOR /F "tokens=4" %%G IN ('dir /A-D /-C ^| find ".exe"') DO start "" /b %%G
This execute every .exe file in your current directory. same as
*.exe
would have done if * were supported on batch.
If you want to execute it directly from a command line window, just do
FOR /F "tokens=4" %G IN ('dir /A-D /-C ^| find ".exe"') DO start "" /b %G
Don't blame their codes for space issue. You should know how to use double quotation marks.
for /f "delims=" %%a in ('dir /b /s *.exe') do (
start "" "%%a"
)

Resources