I have three images named Apple, Banana, Orange
and I also have three folders according to the same name.
And all the files and all the empty folders are on the same path.
Now I want to create a way which can help me to automatically move all the files in the same-named folder.
How to do that?
Use the for loop to do this:
for /f "tokens=*" %%f in ('dir /b /a-d') do move "%%f" "%%~nf"
If you want any file containing "Apple" to be moved to the "Apple" folder, as well as any file containing "Orange" to the "Orange" folder, etc. (even if they contain additional characters in their name such as Apple_1, apple001, green-apple etc.), use the following syntax:
for /f "tokens=*" %%d in ('dir /b /ad') do for /f "tokens=*" %%f in ('dir /b /a-d "*%%d*"') do move "%%f" "%%d"
Related
I'm writing a code to organise a set of files within a folder by creating folders with the original file names in a different folder
I've written the first set of code that organises the files into folders but would like to exclude .Bat files from the code ( so that the batch file itself isn't organised into folders and stays in the %cd%) I also need to adjust the output to a different location
#echo on
setlocal
set "basename=."
for /F "tokens=1* delims=." %%a in ('dir /B /A-D ^| sort /R') do (
set "filename=%%a"
setlocal EnableDelayedExpansion
for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
set "basename=!filename!"
md "!basename!"
)
move "!filename!.%%b" "!basename!"
for /F "delims=" %%c in ("!basename!") do (
:nextprogram
endlocal
set "basename=%%c
)
)
expected results: run this batch file in a folder with files. All files are put into their individual folders with the file name now the parent folder name. Ignore .bat file and move output to a different location.
You could simplify your script and even make it a one liner, for example:
#For /F Delims^=^ EOL^= %%A In ('"Dir /B/A-D 2>NUL|Find /V "%~nx0""')Do #RoboCopy . "%%~nA" "%%A" /Mov>NUL
If you wanted to still perform the reverse sorted name order, which serves no purpose, then:
#For /F Delims^=^ EOL^= %%A In ('"Dir /B/A-D/O-N 2>NUL|Find /V "%~nx0""')Do #RoboCopy . "%%~nA" "%%A" /Mov>NUL
If you're wanting to move the files to another location then something like this should do it:
#For /F Delims^=^ EOL^= %%A In ('"Dir /B/A-D 2>NUL|Find /V "%~nx0""')Do #RoboCopy . "C:\SomeOtherLocation\%%~nA" "%%A" /Mov>NUL
And if you wanted to exclude all .bat files instead of just the running script:
#For /F Delims^=^ EOL^= %%A In ('"Dir /B/A-D 2>NUL|FindStr /LIE ".bat""')Do #RoboCopy . "%%~nA" "%%A" /Mov>NUL
Finally, if you wanted to exclude all batch files, (.cmd and .bat):
#For /F Delims^=^ EOL^= %%A In ('"Dir /B/A-D 2>NUL|FindStr /VIEC:".bat" /C:".cmd""')Do #RoboCopy . "%%~nA" "%%A" /Mov>NUL
To exclude your batchfile, stick it somewhere else. An old practice was to create a directory called C:\Belfry as the obvious place to store bats, and add it to your Path environment variable. You can then run it from any Dos window in any directory on that machine.
To change the destination location, why not change
set "basename=!filename!"
to
set "basename=C:\otherlocation\!filename!"
Even if you are not in that directory, or even on another drive letter, you can still create subdirectories and move files with the full path. Just make sure it exists first.
And, if it you want to move to a dynamic location, take an input to your batch file to choose that directory.
I use following command to copy files to folders with same name.
for /f %x in ('dir /ad /b') do move %x*.* %x\
I use move %x*.* %x\ to move the files to given directory. It doesn't work for files, which has spaces in their filenames, but not sure what to change.
This command uses "DELIMS=" to prevent split.
for /f "DELIMS=" %x in ('dir /ad /b') do move "%x*.*" "%x\"
I have a directory with a list of files, I would like that my batch finds the most recently added file and then copies the content in another file in another directory. So far I did like this:
for /f "delims=" %%x in ('dir /od /a-d /b c:\dir1\*.log') do type %%X >> c\dir2\all.log
The problem is that in this way it outputs for example
ex150422.log 1>>c:\dir2.log
but it doesn't copy the content..
What am I doing wrong?
Thanks!
are you shure it can find the file? add the directory name like you did in dir-command
for /f "delims=" %%x in ('dir /od /a-d /b c:\dir1\*.log') do (
type c:\dir1\%%x>>c:\dir2\all.log
)
The following code makes in each folder, that is placed in the same directory where the code is located, a folder named "VIDEO_TS".
FOR /F "delims=|" %%i IN ('DIR /A:D /B') DO mkdir "%%i"\VIDEO_TS
Like this:
D:\MOVIES
---------------\Bikini Spring Break
-----------------------------------------\P2HBSB5.jpg
-----------------------------------------\VTS_01_1.VOB
-----------------------------------------\VTS_02_0.BUP
-----------------------------------------\VTS_02_0.IFO
-----------------------------------------\VIDEO_TS
----------------\Breaking the Girls
----------------------------------------\pdnydtd.png
----------------------------------------\VTS_02_1.VOB
----------------------------------------\VTS_03_0.BUP
----------------------------------------\VTS_06_0.IFO
----------------------------------------\VIDEO_TS
And the following code puts all the files in each folder in it's "VIDEO_TS" folder.
FOR /F "delims=|" %%i IN ('DIR /A:D /B') DO MOVE "%%i"\*.* "%%i"\VIDEO_TS
My intention is to add both codes together.
And instead of all the files would be placed in their "VIDEO_TS" folder, only the following files in this folder should be moved to the "VIDEO_TS" folder: ".IFO", ".BUP", and ".VOB".
I want to achieve this:
D:\MOVIES
---------------\Bikini Spring Break
----------------------------------------\P2HBSB5.jpg
----------------------------------------\VIDEO_TS
-------------------------------------------------------\VTS_01_1.VOB
-------------------------------------------------------\VTS_02_0.BUP
-------------------------------------------------------\VTS_02_0.IFO
----------------\Breaking the Girls
-----------------------------------------\pdnydtd.png
-----------------------------------------\VIDEO_TS
--------------------------------------------------------\VTS_02_1.VOB
--------------------------------------------------------\VTS_03_0.BUP
--------------------------------------------------------\VTS_06_0.IFO
This should do what you want.
FOR /F "delims=|" %%i IN ('DIR /A:D /B') DO (
mkdir "%%i"\VIDEO_TS
FOR %%a IN (VOB BUP IFO) DO MOVE "%%i"\*.%%a "%%i"\VIDEO_TS
)
This question might have a simple solution, but I just cannot find it.
Let's say I'm using the DIR command to produce a list of .txt files in the folder "E:\Documents". How can I make sure that only the .txt files one level below "E:\Documents" are listed (i.e. in a direct subfolder) and not the files in "E:\Documents" itself. "E:\Documents\\" or "E:\Documents\*\" doesn't seem to do the trick.
Thanks in advance!
FOR /f "delims=" %%i IN ('dir /b/ad') DO IF EXIST ".\%%i\*.txt" DIR ".\%%i\*.txt"
That's if you want the output in DIR format - with headers
FOR /f "delims=" %%i IN ('dir /b /ad') DO IF EXIST ".\%%i\*.txt" (
FOR /f "delims=" %%q IN ('DIR /b ".\%%i\*.txt" ') DO ECHO ".\%%i\%%q"
)
if you want just the filenames.
This can be achieved without dir:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~ff
If you need not only the file names, but also detail information about the files, add the respective qualifiers, e.g.:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~azff
a → attributes of the file
t → timestamp of the file
z → size of the file
See help for for details.