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"
Related
I'd like to list all .csv files in a directory and its subdirectories. It works nicely when I run this in the cmd terminal:
for /f %a in ('dir /b /s *.csv') do (echo %a)
When I put the same line of code into an (otherwise empty) text file (.cmd) and run that .cmd file, it outputs a blank line, but does not list any files.
Why does it make a difference whether I put the commands into a script or enter them directly (in one go) in the terminal?
Thinking it might be a delayed expansion issue, I also tried for /f %a in ('dir /b /s *.csv') do (echo !a!), but this doesn't list the files either.
Any ideas?
Double up the percent symbols in a batch file!
for /f %%a in ('dir /b /s *.csv') do (echo %%a)
I am looking for a single windows command where I need to delete all the files in a folder except one.
eg: Files listed under directory:
c:\users\admin\folder
abc
abcde.zip
abcdef
123.txt
Now, I want to delete all the files except "abcde.zip"
corresponding Linux command would be: rm -rf !(abcde.zip)
Is there anything like this on windows without using batch script, just a single line command?
Thanks in Advance!
Since Win's command interpreter doesn't have a rm -rf like command (that works on both files and dirs) we just need to modify the command that I placed in my 1st comment, actually split it in 2 commands (still in a single line: well, if the console is really really wide :) ), and execute them in a sequence (&):
one that takes care of the files (the /a:-d argument for dir), and launches del
one that takes care of the folders(the /a:d argument) and launches rmdir
Here's what it looks like (note that it does the job for the current directory):
(for /f "tokens=*" %f in ('dir /a:-d /b 2^>nul ^| findstr /v /b /e /c:"abcde.zip"') do (del /f "%f")) & (for /f "tokens=*" %f in ('dir /a:d /b ^| findstr /v /b /e /c:"abcde.zip"') do (rmdir /q /s "%f"))
I'm trying to put together a Windows batch script that moves the most recent file to a different directory. Here's my batch script mostly stolen from here:
FOR /F "tokens=*" %%G IN ('dir /b *.*') DO move %%G C:\Users\jrobinson\Desktop\ & exit /b
When I run this it moves every file in the folder. Interestingly, when I replace the MOVE command with a COPY command, the script copies only one file:
FOR /F "tokens=*" %%G IN ('dir /b *.*') DO copy %%G C:\Users\jrobinson\Desktop\ & exit /b
REM ---------------------------------------^^^^
Why is the move script moving every file while the copy script is only copying one file? Is it possibly because my script file is also in that folder and because of my edits, it is the most recent file?
Edit: Here are the contents of my source folder:
myBatchFile.bat
newest.txt
oldest.txt
second.txt
Of the *.txt files, newest.txt has the most recent modified date. Of all of the files regardless of extension, myBatchFile.bat has the most recent modified date given that I keep making edits to it to try to get it to work.
Edit #2: Here's a screenshot of my command window after running the first command above. This shows that all of the files in my source folder are copied when I only expect the newest file to be copied.
I'd use dir /b /o:-d /a:-d *.*, so the items are sorted by date (newest first) and directories are excluded. Otherwise the sort order is not clear, and directories like . (current) or .. (parent) might be returned unintentionally.
Instead of %%G I recommend using "%%~fG" to provide the full path, surrounded by doublequotes. Also the destination path should be enclosed in "" to avoid troubles with whitespaces.
The FOR /F should be changed to "delims=" in case the file name starts with a space (which I think is valid in Windows). "tokens=*" removed them.
Furthermore, I'd put parenthesis () around the code after do (so it is guaranteed and obvious that both commands move and exit /b are part of the loop context).
So all in all this leads to the following code:
FOR /F "delims=" %%G IN ('dir /b /o:-d /a:-d *.*') DO (move "%%~fG" "C:\Users\jrobinson\Desktop\" & exit /b)
Since the newest file is enumerated first and the for body contains exit /b (exit batch file), only that file is moved.
If you need to execute some other code after this line, replace exit /b by goto :SKIP (or any other valid label), and state the label (:SKIP) immediately after the FOR command block.
If the batch script containing the code herein is located in its working directory, moving it (in case it is the newest file) most likely results in unexpected behaviour. You can exclude the batch script itself by filtering it out using find like this:
FOR /F "delims=" %%G IN ('dir /b /o:-d /a:-d *.* ^| find /v /i "%~nx0"') DO (move "%%~fG" "C:\Users\jrobinson\Desktop\" & exit /b)
%~nx0 therein expands to the file name and extension of this script (see call /?).
I'm not sure I believe what you say. Because when I type the same command, is a file that I see first followed by the PAUSE command (or exit)
FOR /F "tokens=*" %G IN ('dir /b *.*') DO #echo %G & pause
So it always goes first action on the first file and the script will stop.
See this 5 secs gifv: http://i.imgur.com/kyHdN1Y.gifv with the following code:
#echo off
if exist ..\destination-test-folder\nul rd /s /q ..\destination-test-folder
if not exist ..\destination-test-folder\nul md ..\destination-test-folder
copy nul newest.txt>nul
copy nul oldest.txt>nul
copy nul second.txt>nul
dir /b .
pause>nul
echo: &echo For loop command: &echo:
for /f "tokens=*" %%g in ('dir /b "*.*"') do if "%%g" neq "%~nx0" move %%g ..\destination-test-folder>nul & echo pause is stated after each iteration & echo 1 file(s) moved. &pause>nul
echo: &echo list destination:
dir /b "..\destination-test-folder" &echo: &echo list source: &echo:
dir /b .
Just wanted to say thanks person: aschipfl
You are my hero.
here is the command I used to move all read only files to a sub folder "temp"
md temp
FOR /F "delims=" %%G IN ('dir /b /o:-d /a:R *.*') DO (move "%%~fG" "temp" )
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.
Is it possible to get the name of the last created folder in a variable on the windows command line?
Preferably natively, without using Cygwin etc. What I'm looking for is to run some command/batch script which gives me the LastCreatedDir for the path in which I currently am
from a batch file:
#echo off
FOR /F "delims=" %%G IN ('dir /O:-D /A:D /T:c /B') DO (
#set yourvariable=%%G
exit /b
)