Move files to folders with same name in Windows - windows

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\"

Related

How to move multiple files in multiple folder in a simple method

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"

Get path of found file in for loop

I want to loop through a folder and its subfolders, get all .jpg files and run myapp.exe on the files where I rename the currently found file's extension. myapp.exe takes the full file path as input and a renamed file as output, like so:
myapp.exe C:\images\original\210_551_210-768--RJF3823klzw.jpg -o C:\images\original\210_551_210-768--RJF3823klzw.gif
I now have this to recursively print the full path of *.jpg files.
for /f "delims=" %%i in ('dir /a-d/b/s *.jpg') do echo "%%i" "%%~ni.gif"
However, this prints only the filename for the second parameter, whereas I want to include the path of the found file.
How can I include the path of the output file, the second parameter for myapp.exe?
Pseudocode:
for /f "delims=" %%i in ('dir /a-d/b/s *.jpg') do myapp.exe "%%i" "<FULLPATH><FILNAME>.gif"
If myapp.exe actually converts the file instead of simply changing the extension then you can use metavariable expansion, (see For /? for examples):
For /F "Delims=" %%A In ('Dir /B/S/A-D *.jpg') Do myapp.exe "%%A" "%%~dpnA.gif"
If you're only replacing the extension the you can just use the rename command:
For /F "Delims=" %%A In ('Dir /B/S/A-D *.jpg') Do Ren "%%A" "%%~nA.gif"
In either case you could also change your for loop to:
For /R %%A In (*.jpg)

BATCH - Find most recent file and concatenate it into another file

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
)

How to merge these codes, and move certain files instead of all files to a folder?

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
)

Directory list for FOR loop in msdos .bat file

I am looking how to get list of all directories to be used in FOR loop.
So far I have work around:
set folderList = (folder1 folder2 folder3 folder4)
FOR %%i in %folderList% do zip %%i D:\...my_path...\%%i\*.*
is it possible that folderList would be generated dynamically ?
assuming you want to list subdirectories of c:\temp
for /f %%i in ('dir c:\temp /ad /b') do echo %%i
this will list foldernames of c:\temp, if you want get it recursively just add /s to dir command:
for /f %%i in ('dir c:\temp /ad /b /s') do echo %%i
as for #dbenham comment (thank you) to correctly handle dirs with space just add tokens=* to for :
for /f "tokens=*" %%i in ('dir c:\temp /ad /b') do echo %%i
Please try below code:
for /d %%F in ("d:\...my_path...\*") do zip "%%~nxF" "%%F\*.*"
I am not sure what is different but the double %% listed above are all failing to work.
This, however, works for me:
for /f "tokens=*" %i in ('dir c:\temp /ad /b') do echo %I

Resources