Recursively moving files to root folder - batch-file

I am more used to using unix shell than CMD, and I am not really sure how to get this to work. I have a directory with several other sub-directories that contain .xml files. I would like to move all the files recursively to the root directory. I know with unix this done like so:
find FOLDERPATH -type f -name '*.xml' -exec mv -i {} FOLDERPATH \;
Yet I can't seem to find something that will work in the same way. XCOPY looked promising, but it doesn't copy only the folders, it copies the whole structure, thus I get these sub-directories that I don't want again. Has anyone got any other suggestions?

This will work from the CMD prompt. Run it in the folder you want the files to be moved to, and it will process the sub-directories in that folder.
It does not provide a mechanism to handle filename collisions elegantly.
for /R /D %f in (*) do move "%f\*.xml" .
and this will work in a batch file.
#echo off
for /R /D %%f in (*) do move "%%f\*.xml" .

Try this:
set FOLDERPATH=...
for /R "%FOLDERPATH%" %%f in (*.xml) do move "%%~ff" "%FOLDERPATH%"

Related

Write checksum per folder

I'm trying to write a script that will run through a directory tree and generate a checksum for the contents of each folder and write the checksum to that folder; I've managed to do everything with the following code except write the checksum to the folder, it's currently writing everything to the root directory.
FOR /R "C:\_input\test" /D %%a IN (*) DO md5deep64 -r "%%a" >> "%%a.md5"
I thought I might be able to do something with the various modifiers (%~I) but no joy. Any ideas?
Based on your latest comment and my own, I think this may be what you want:
As a batch file:
#For /D %%A In ("C:\_input\test\*) Do #md5deep64 -r "%%A">"%%A.md5" & #Move /Y "%%A.md5" "%%A"
At the command line:
For /D %A In ("C:\_input\test\*) Do #md5deep64 -r "%A">"%A.md5" & #Move /Y "%A.md5" "%A"
Note that md5deep64.exe would need to be in the current directory or %Path%, otherwise you'd need to provide the full or relative path to it.

How to delete all files from "Output" folder using batch script?

I have a folder structure as below:
L:\MyProject\Extract\
Activity Input
**output**
SSIS
Results
Headers
Account Input
**output**
SSIS
Results
Headers
Balance Input
**output**
SSIS
Results
.
.
.
Here, I need to find output folder and delete all files present inside each "output" folder. I am using following script:
SET FolderPath=%1
for /f "usebackq" %%a in (`"dir %FolderPath% /ad/b/s output"`) do SET Folder="%%a"
del %Folder%\*.* /Q
But using above script, I am able to delete files which are present in "%%a"(last path of output folder). How to do it recursively...? Or is there any array concept to store all paths where output folder is present?
I see three problems in your code.
First, the for loop does not find the output subdirs under FolderPath, but iterates over all the subdirs of FolderPath.
One way to solve this is to specify
for /f "usebackq" %%a in (`dir "%FolderPath%\output" /ad/b/s`) do
Note that I have moved the quotes, just to prepare for the case of names with spaces, which was your second problem. You might further simplify it with
for /f "usebackq delims=" %%a in (`dir "%~1\output" /ad/b/s`) do
note that delims avoids spaces work as delimiters and %~1 removes the quotes of the parameter
And finally, the for loop, in each iteration, just sets the Folder variable, it does not invoke the del command, that is placed outside the loop.
One easy way to solve this problem is to invoke the del command on %%a without setting the variable folder
for /f "usebackq delims=" %%a in (`dir "%~1\output" /ad/b/s`) do del %%a\* /q
You could use the find command to find all subdirectories of the current directory.
To list files in all directories named Output:
find . -name Output -type d|find -type f -exec ls {} \;
To delete all files in directories named Output:
find . -name Output -type d|find -type f -exec rm {} \;

BAT File to find most recent files based on filename

I need to be able to create a bat script to do 3 things:
Search for multiple specific filenames in a directory.
Find the most recently generated version based on each filename specified.
Copy that most file to a new dir.
I am very new to coding in general, so any assistance would be much appreciated.
So far all I have been able to do is figure out how to copy files from one location to another using the below:
xcopy /s c:\source\differentfilename1.csv d:\target\
xcopy /s c:\source\differentfilename2.txt d:\target
xcopy /s c:\source\differentfilename3.html d:\target
So far I have tried the following and its not copying the files over:
ECHO
CD D:\Data\
MKDIR D:\Data\CopyFilesHere
for /R %file in (Filename1.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename2.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename3.*) DO XCOPY "%file" D:\Data\CopyFilesHere
I have since noted there are subfolders I need to search through also.

How to delete a file from multiple zip archives using 7-Zip

I have a thousand zip archives that all contain a file I want to remove. I can get 7Zip to remove them one file at a time from the command line:
7z d -r archive.zip *.pdf
but how would I apply that across all the files, which are themselves grouped in sub-directories?
Try this:
for /r %v in (*.zip) do 7z d -r "%v" *.pdf
But no idea if it's working, just wrote out of my head :P
FOR /F "tokens=*" %%G IN ('dir /b *.zip') DO 7z.exe d -r %%G *.pdf
This works almost in the same way as the accepted answer. Only the way how the files are gathered is different. Whereas the answer above uses for /r to run through all directories and subdirectories, this one parses the output of the command dir /b *.zip to get all the files relevant.
The 7zip command remains the same and only the parameters are changed.
Note: To run this outside of a batchfile replace %%G with %G
Sometimes simple things can solve the problems...
do the following to delete any file / all file from selected .zip files>
Move the .zip files to a new folder
Select all the files from which files to be deleted
right click and select option "extract each archive to separate folder"
all the zip files shall be converted to folder now.
use and file search tool like SearchMyFiles and find required files, select and delete.
convert the folders back to .zip

copy all files recursively into a single folder (without recreating folders)

With a batch (.bat), I want to copy all mp3 files that are in 1 subdirectory of D:\TEMP
D:\TEMP\\(anyfolder)\\(anyfile.mp3)
to
E:\MYFOLDER\
I tried with xcopy but
I don't know how to tell "just recurse subfolders of D:\TEMP and not subsubfolders, subsubsubfolders, etc."
When using xcopy, folders are created in the destination (in order to replicate source's folder tree), I don't want this : files should be copied in just 1 single folder.
for command is your friend. Read help for and then try this in the command prompt
for /d %a in (*) do #echo %a
as you see, it follows all subfolders in the current directory.
thus,
for /d %a in (*) do #copy %a\*.mp3 e:\myfolder
will copy all your mp3 to the destination folder.

Resources