Batch file "for" command - windows

Need to be able to "schedule" a command line action using windows scheduler.
This is my command:
for /r C:\Users\bob\Downloads\Done\ %f in (*) do #move "%f" C:\Users\bob\Downloads\Done\
It flattens all files into the main folder.
The "for" command does not work as in a scheduler but is working in cmd.exe
I need a batch file or other code so that I can use the scheduler.
I read these, but I'm just not that cool:
How to copy files from folder tree dropping all the folders with Robocopy?
How does "FOR" work in cmd batch file?

Take your above code and save it in a .bat file. The only change you need to make, is that percents (%) need to be doubled-up when run inside a batch file.
for /r C:\Users\bob\Downloads\Done\ %%f in (*) do (
#move "%%f" C:\Users\bob\Downloads\Done\
)

Related

Batch file to create a text file with the same name as every .zip file in a directory

I'm not very familiar with windows batch scripts except very simple applications. I'm currently trying to figure out how to run a batch file in a local directory, scan the directory for each *.zip file and then create a corresponding .txt file (in the same local directory). This will run indefinitely at intervals of say... 5 seconds. Any clues to help? Would it be easier to write a Java program or something?
You can use the FOR command to iterate through files in a directory. The TYPE command creates an empty file.
#echo off
FOR %%G IN (*.zip) DO TYPE nul>"%%~nG.txt"
I got it:
#echo off
:loop
FOR %%G IN (*.zip) DO TYPE nul>"%%~nG.txt"
timeout /t 300
goto loop

exclude *.lyx~ files in batch (windows)

I just created the following batch file which saves all my lyx documents as tex files:
cd /d "D:\"
:: if the "for"-command is executed from the command line, just use "%" rather than "%%"
for /R %%g in (*.lyx) do "C:\Program Files (x86)\LyX 2.1\bin\lyx.exe" --force-overwrite --export pdflatex "%%g"
The problem is now that instead of the *.lyx files the batch uses the *.lyx~ files which are as far as I know some kind of backup files and don't contain the latest content.
How can I modify the batch file such that it takes the *lyx files rather than the *.lyx~ files?
Any suggestion would be great help.
Best :-)
PS: If I type this code in the command line (without using the batch), everything is fine.
#Edit1: added tokens option. Now it works in subdirs with spaces.
Modify your for loop to:
for /F "tokens=*" %%g in ('dir /b /s *.lyx') do if "%%~xg" equ ".lyx" (
"C:\Program Files (x86)\LyX 2.1\bin\lyx.exe" --force-overwrite --export pdflatex "%%g"
)
Another solution would be to use forfiles.
As for the 3rd comment, I think it does both but it does the .lyx~ at the end.
To see the output of a batch file simply launch it from a cmd window.

Command line to change file extension of all files under a folders and its subfolders

I am looking for a command line which can change the file extension of all the files that are under a folder and its subfolders. Is there any way to do this?
I tried with ren *.js *.txt but this only changes the file extension of the files under one folder.
I assume by ms-dos you really mean the command prompt on Windows. Not many people still use ms-dos.
The following will run your REN command on each folder within the hierarchy that contains .js files. It is probably a bit more efficient then running REN for each file individually.
for /r %F in (.) do #if exist "%F\*.js" ren "%F\*.js" "*.txt"
Double up the percents (%F becomes %%F) if run within a batch script.
You could try this:
For /R %G in (*.js) do REN "%G" "%~nG.txt"
Note that you'll need to use %% instead of % if running from a batch file.

Want to execute command on each file in directory one at a time through batch file windows

I am currently doing this to execute a single command on a particular type of files in directory.
COPY *.prn /B \\\\{$PC}\\{$PRINTER}
The PC And Printer Part is redundant no need to understand that
Instead of executing all files at once I want to be able to do one file at a time through a loop
try this:
for %%i in (*.prn) do COPY "%%~i" /B \\\\{$PC}\\{$PRINTER}
Im not entirely sure what you mean but try this, it will execute the command once for each file in the current directory and (all subdirectories, but this exact snipets not ideal for subdirectories) ending with the extension .prn:
for /r %%a in (*) do (
if %%~xa == .prn (
copy %%~na%%~xa /B \\\\{$PC}\\{$PRINTER}
)
)
Tell me if this doesn't work or you want to do this for subdirectories as well.
Yours, Mona

Creating Bat file to execute a command on all files in folder

How to create a bat file that will execute a single command on all files with a particular extension.
I look for something like this which is there is linux for windows batch files
command *.extension
or even a way to loop through the file with extension would do.
If your command changes the filename and uses the same extension then this solution has a bug, and some files are processed more than once. There are ways around that.
#echo off
for %%a in (*.ext) do (
echo "%%a"
)

Resources