Say I have a folder "C:\pokepoke" with archives like:
trequetry.part01.rar
trequetry.part02.rar
trequetry.part03.rar
and
replokitro.part01.rar
replokitro.part02.rar
replokitro.part03.rar
replokitro.part04.rar
and so on. How can I make it so the first set of archives ends up in C:\pokepoke\trequetry and the second in C:\pokepoke\replokitro and so on. So basically filter on X.partX.rar or something in that direction.
I am trying to batch-create recovery files with par2 for lots of split-up archives so in addition to my main question, I would also like to find out how to perform a for-each on all subfolders in C:\pokepoke so I can run the following code on it once all archives are moved to their respective folders:
FOR /R %%g IN (.) DO C:\WINDOWS\par2.exe c -r10 -s384000 "%%g\%%~ng.par2" "%%g\*"
del /q *.par2
To help you with your first question, the following script worked for me:
#ECHO OFF
SET "origloc=D:\path\to\archives"
FOR %%F IN ("%origloc%\*.part*.rar") DO CALL :process "%%F"
GOTO :EOF
:process
CALL :checkpath "%~dpn1"
MOVE %1 "%subfolder%" >NUL
GOTO :EOF
:checkpath
SET "subfolder=%~dpn1"
IF NOT EXIST "%subfolder%\" MKDIR "%subfolder%"
GOTO :EOF
This script searches for *.part*.rar files in the specified folder. It applies the ~dpn modifier to every name twice to strip the name of the 'double extension' .partNN.rar, then uses the resulting name as the subfolder name.
Optionally you could change the first SET command like this:
SET "origloc=%~1"
to be able to call the script for an arbitrary path, passing the path as a parameter.
Related
I have a folder named "Projects". In that folder, I have multiple folders for my music projects, that have different names, and in each one of these I have an FL Studio project file (.flp), along with project's data, like Wave and MP3 files. But all of the FLP files have the default name ("untitled.flp"). Is there a way to change file names to names of directories that they're in?
I thought that I can make a batch or VBS file, but I'm not sure how should I program it.
Pseudo batch code:
:loop
for each "untitled.flp" in "D:\Google Drive\Projects" goto change
exit
:change
set variable="untitled.flp directory"
ren "untitled.flp" %variable%
goto loop
I'm running Windows 10.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /r "%sourcedir%" %%a IN (untitled.flp) DO IF EXIST "%%a" (
FOR %%s IN ("%%~dpa.") DO ECHO REN "%%a" "%%~nxs.flp"
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
The first for assigns the directoryname from the entire tree to %%a and appends the filename. The if gates out directorynames where the file is absent and the inner if constructs the full pathname of %%a and appends ., treating the entire resultant name as a filename; then the ren command renames the full filename of the file found to the name and extension part of the file's parent directoryname.
I want to be able to use the "Send to" function (When right clicking a file) with this batch file.
It needs to create a folder, with the name of the file, for each of the selected files, in the same directory as the file itself. (No moving of the file needed)
The following code has helped, but this creates folders for all files in the directory and places it in the directory of the batch file.
#echo off
pushd %~dp0
for /f "delims=" %%a in ('dir /b') do (
if not "%%~fa"=="%~f0" (
md "%%~na" 2>nul
)
)
popd
I believe using the following function will be needed for the directory of the files but not sure about how to call it.
%CD%
I am rather new to batch files so any extra explanation would be helpful, but not necessary.
Even if it can only run on one file at a time, that will be great since it needs to be no a chosen file basis.
Here goes to learning on the go and thanks for any help!
This should do what you are looking for. Give this script a try in your Send To menu:
#ECHO OFF
SETLOCAL
:ProcessFile
REM Check if there are any files to process.
IF "%~1"=="" GOTO :EOF
REM Process the current file.
SET NewDir="%~dpn1\"
REM Create the directory if it doesn't already exist.
IF NOT EXIST %NewDir% MKDIR %NewDir%
REM Move to the next selected file.
SHIFT /1
REM Recurse.
GOTO ProcessFile
ENDLOCAL
I'm trying to write a batch to rename multiple folders. I have got the below script which I'm pretty sure will work if I target only the desired folders in the directory. I want to remove characters from a folder name so zz_name can be renamed to name.
The problem is my script is looking on the first 3 characters and rename all folders, not just those with zz_. I'm thinking at worst I could move the zz_ folders to their own directory, rename them, and move them back. But that seems like a long way to go.
Is it possible to target only zz_ in folder name?
setlocal
for /d %%i in (*) do call :rename %%i
goto :eof
:rename
set ZZDIR=%1
ren "%ZZDIR%" "%ZZDIR:~3%"
Adam Smith has answered already the question.
However, here is an improved batch code for this question.
#echo off
setlocal
for /d %%i in (zz_*) do call :RenDir %%i
endlocal
goto :EOF
:RenDir
set "ZZ_DIR=%1"
set "NEWDIR=%ZZ_DIR:~3%"
if not "%NEWDIR%"=="" if not exist "%NEWDIR%" ren "%ZZ_DIR%" "%NEWDIR%"
This batch file ignores a directory with just zz_ as name.
And it also does not try to rename for example zz_xxx to xxx if there is already a directory xxx.
I have a folder named x with a number of subfolders and files. I want to delete a folder named y that is present in x and all of it's subfolders. The said folder that has to be deleted may or may not contain any files. I believe i can do this using cmd or some kind of batch file but i am a command line new bi and can really use some help.
A simple thing would be to rd the folder's name, which works but i believe there are better ways than removing each folder individually.. like some loop that goes through all the folders.
Thanks
EDIT: Just to clarify, i have y (the folder that needs to be deleted) inside of x, and it can be in any of x's subfolders and at any level of depth. Also i am looking at answers and it may take some time for me to accept any answer. Please bear with me :)
Here is another solution for this commented to describe each part of the script:
#Echo OFF
REM Important that Delayed Expansion is Enabled
setlocal enabledelayedexpansion
REM This sets what folder the batch is looking for and the root in which it starts the search:
set /p foldername=Please enter the foldername you want to delete:
set /p root=Please enter the root directory (ex: C:\TestFolder)
REM Checks each directory in the given root
FOR /R %root% %%A IN (.) DO (
if '%%A'=='' goto end
REM Correctly parses info for executing the loop and RM functions
set dir="%%A"
set dir=!dir:.=!
set directory=%%A
set directory=!directory::=!
set directory=!directory:\=;!
REM Checks each directory
for /f "tokens=* delims=;" %%P in ("!directory!") do call :loop %%P
)
REM After each directory is checked the batch will allow you to see folders deleted.
:end
pause
endlocal
exit
REM This loop checks each folder inside the directory for the specified folder name. This allows you to check multiple nested directories.
:loop
if '%1'=='' goto endloop
if '%1'=='%foldername%' (
rd /S /Q !dir!
echo !dir! was deleted.
)
SHIFT
goto :loop
:endloop
You can take the /p out from in front of the initial variables and just enter their values after the = if you don't want to be prompted:
set foldername=
set root=
You can also remove the echo in the loop portion and the pause in the end portion for the batch to run silently.
It might be a little more complicated, but the code can be applied to a lot of other uses.
I tested it looking for multiple instances of the same foldername qwerty in C:\Test:
C:\Test\qwerty
C:\Test\qwerty\subfolder
C:\Test\test\qwerty
C:\Test\test\test\qwerty
and all that was left was:
C:\Test\
C:\Test\test\
C:\Test\test\test\
FOR /D /R %%X IN (fileprefix*) DO RD /S /Q "%%X"
Take care of using that...
for RD command:
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
/Q Quiet mode, do not ask if ok to remove a directory tree with /S
the FOR command is used to loop through a list of files or variables, the options are very easy to memorize, Directory only Recursively.
A problem common to this type of topics is that if there are instances of the target folder at several levels, most methods cause an error because when an high level folder is deleted, all folders below it disappear. For example:
C:\X\Y\subfolder
C:\X\Y\subfolder\one\Y
C:\X\Y\subfolder\two\Y
C:\X\Y\subfolder\three\Y
C:\X\test
C:\X\test\test
Previous example generate a list of 4 folders named Y that will be deleted, but after the first one is deleted the three remaining names no longer exist, causing an error message when they are tried to delete. I understand this is a possibility in your case.
To solve this problem the folders must be deleted in bottom-up order, that is, the innermost folder must be deleted first and the top level folder must be deleted last. The way to achieve this is via a recursive subroutine:
#echo off
rem Enter into top level folder, process it and go back to original folder
pushd x
call :processFolder
popd
goto :EOF
:processFolder
rem For each folder in this level
for /D %%a in (*) do (
rem Enter into it, process it and go back to original
cd %%a
call :processFolder
cd ..
rem If is the target folder, delete it
if /I "%%a" == "y" (
rd /S /Q "%%a"
)
)
exit /B
Although in this particular case the problems caused by other methods are just multiple error messages, there are other cases when this processing order is fundamental.
Make .bat with following:
del /q "C:\Users\XXX\AppData\Local\Temp\*"
FOR /D %%p IN ("C:\Users\XXX\AppData\Local\Temp\*.*") DO rmdir "%%p" /s /q
I can't find any references explaining how to loop through a folder passed as an argument and rename each file. All examples I've seen assume the script is running in the folder where files will be renamed or hardcode the folder path in the loop. How can I do this? Here is an example of what I'm trying to do:
for /f %%a in (%1) do call :RenameFiles
:RenameFiles
Rename %%a "new filename"
Goto :EOF
The following adds prefix "renamed-" to every file in the directory specified as a command line argument:
for %%F in (%~1\*) do ren "%%~F" "renamed-%%~nxF"
EDIT The simplest solution to the problem of the infinite loop is to work in two stages:
prepare the list of operations
execute the plan.
-
set OpList=%TEMP%\%~n0%RANDOM%.bat
copy nul "%OpList%"
for %%F in (%~1\*) do echo ren "%%~F" "renamed-%%~nxF" >> "%OpList%"
call "%OpList%"
del "%OpList%"