Batch Script to Rename Files to "Raw.txt" in Subfolders - batch-file

Say I have a set of Subfolders, in which I have some files in them (the name of the files does not matter, nor does the file extension). How would I write a batch script to iterate over these subfolders so that I can rename all the files in them to "raw.txt". I have no idea how to approach this, I am only familiar with the "ren" command

With this command you can iterate recursively over all the files from a give directory:
for /r <rootdir> %i in (*) do #ren "%i" raw.txt
Be aware that if there is more than one file in a folder ren will not change the file's name to raw.txt. Hope it helps.

Related

Making a batch file to zip contents of a folder in seperate zips

I need a batch file to zip the contents of the current directory into individual zip files (each file has its own zip file). I'm interested in learning more about making batch files so help in breaking down what the script means would be appreciated. Below is a batch to zip subdirectories in the current directory, I was going to try to edit it to zip individual files of a directory but I don't really know how to and I can't find a reference sheet or anything. So if you have a link for something similar that would be great.
for /d %%X in (*) do "C:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\"
What I think I understand here is:
'for /d' will loop the batch through the directory
'in (*)' is designating the current directory
'do "executable location"' is designating the process
'a "%%X.zip"' is designating the file type?
I don't really understand what "%%X" is
Thank you in advance.
If you want to change what you have from zipping subfolders to zipping files, just remove the /d, then it will look for files instead of directories.
for %%X in (*) do "C:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X"
To answer your question about what %%X is: That's the variable. When you go into the loop using for, you need some variable to represent whatever it is you're looping through.
As you go through the loop, %%X represents whatever file you're working on.
So if the first file was File1.txt, then the first time through the loop, %%X would equal File1.txt.
So the command would be: "C:\Program Files\7-Zip\7z.exe" a "File1.txt.zip" "File1.txt"
Which if I recall the 7-zip arguments, you're saying add File.txt to a zip file named File1.txt.zip. The next time through the loop, %%X represents the next file name.
By the way, if you're running this in the CLI ('command prompt' essentially), then you'd use a single %, for batch files you have to use two (%%).

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.

move files to one directory

DOS bat
I want to find files with a certain extension, for example
where /R c:\ *.ppx
and move them all to a specified directory, for example c:\PPS
Thanks!
Redirect the output of WHERE to a file
WHERE /R C:\ *.ppx > ppxlist.txt
And then use a FOR loop to move them
FOR /f %i in (ppxlist.txt) do move %i c:\pps\
Here's a similar question that suggests a few methods:
How can I recursively copy files of a specific pattern into a single flat folder on Windows?

Create batch file to unzip files in multiple folders

I am trying to create a batch file that searches a folder for zipped files, and unzip them. I want the script to search through all the subfolders in the main folder, and unzip everything it finds. It might be a main folder with several subfolders. Some of the subfolders will contain zipped files, but some will not.
The zipped files will look like "filename.r00", "filename.r01", "filename.r02", and so on. One file will have the name "filename.rar", and it is this file that will need to extracted using 7-zip.
Is it possible to write a batch file that does this, and then deletes all the zip files? I have already installed 7-zip, so if it is possible I would like to use it. If anyone could help me write the batch file, it would be much appreciated!
Thanks!
Here you go
for /r C:\Mainfolder %%a in (filename.r*) do (
7z e %%a -o%%a_Extracted
del %%a /f /q
)
After looking at the switches for 7-Zip this might be faster (untested)
7z x filename.r* -o*_Extracted -r
del filename.r* /f /q
Either way they are extracted to a folder with _Extracted at the end, otherwise it will extract them to a folder with the same name as the archive, and when it deletes the files, it may try and delete the folders too.

Resources