How to Add prefix to all file in current folder and subfolder using batch file - windows

I am trying to add prefix to all files in current folder and it's subfolders and i made the following batch file
setlocal enabledelayedexpansion
for %%j in (*) do (
set filename=%%j
rename !filename! [nilesh.uk.to]-!filename!
)
it working for current directory only but i want to add prefix to all files in current folder as well as subdirectory please help me to solve this problem

Run this from another folder, and the pushd is used otherwise the batch file will be renamed too. Change c:\mainfolder to your main folder name
#echo off
pushd "c:\mainfolder\"
for /r %%j in (*) do (
rename "%%j" "[nilesh.uk.to]-%%~nxj"
)
popd

using renamer, you can do it in one command like this:
$ renamer --regex --find '^' --replace 'prefix-' '**'

Related

How to auto create folder based on filename and move the file into it's folder using .BAT

I have already solved my question... What I haven't solved is how to do this if the .bat file is located in a parent folder and that it should work on all subfolders?
Right now, there's a limitation that it only create folders if the .bat file is located in the same folder as the files. It can't create folders if the files are inside a subfolder.
What I have is:
the filename of this .bat is :
organize.bat
#echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)
How I do it right now:
I place the .bat file in a folder together with the files
When I click it, it will create folders with a name based on the files inside that folder
It will also move each files in those folders of the same name
What I need:
Place the .bat file in the main folder with many subfolders containing the files
Click it to perform the same tasks above
Apologies if my explanation is confusing... I hope it's still understandable.
Thank you in advance!
Your attempt is very close to working but beware the wrinkles of using a simple approach without checking each detail so, start here:-
#echo off & Title %~n0
REM I recommend using cd /d "%~dp0" to ensure you start from the known cmd file folder location not some system folder
cd /D "%~dp0"
REM add the /R switch to run through subdirs
for /R %%i in (*) do (
REM replace organize to %~n0 so as to aid renaming by other users
if not "%%~DPni" == "%~DPn0" (
REM to allow for nested paths we need a fuller DP location for N (check it works exactly as desired then remove the echos)
echo md "%%~DPni" && echo move "%%~i" "%%~DPni"
)
)
BEWARE files with double .dot.s such as cmd.exe.lnk so check those echo's first
md "C:\Users\me\Favorites\Links\cmd.exe"
move "C:\Users\me\Favorites\Links\cmd.exe.lnk" "C:\Users\me\Favorites\Links\cmd.exe"

Files having same prefix to be moved to another directory

I have a files which doesn't have extension ending A_INI, A_FIF. And I need to write a code to search this files by user entering only A, if Exist I need to copy A_INI, A_FIF to another folder.
Example folder contains A_INI, A_FIF, A_LOG by prompting user enters file name as A.
I have to check files starting with A_* and if exist i have to move it to another folder.
How can this be achieved using batch script?
Not tested:
#echo off
setlocal
set "source_dir=C:\source"
set "destination_dir=C:\dest"
set /p "mask=Enter a pattern: "
pushd "%source_dir%"
for %%# in ("%mask%_*") do (
copy /y "%%~#" "%destination_dir%"
)
endlocal

How can I create new empty folders in a directory using names of subfolders of another directory using batch script?

My main goal is to backup SVN repositories in REPOS folder. Since "svnadmin hotcopy" has to have both source and target folders I need to create new folders in different directory with name of folderName_backup and then copy them using "svnadmin hotcopy". Btw this has to be done in windows batch file. My code for this portion is the following:
for /d %%X in (%source%\*) do (
md %destination%\%%X_backup
svnadmin hotcopy %%X %destination%\%%X_backup
)
After running this code I get error : The filename, directory name, or volume label syntax is incorrect.
set "source=c:\where\the\folders\are"
set "destination=c:\where\the\backup\will\be"
for /d %%a in ("%source%\*") do for %%b in ("%destination%\%%~nxa_backup") do (
if not exist "%%~fb\" md "%%~fb"
if exist "%%~fb\" svnadmin hotcopy "%%~fa" "%%~fb"
)
%%~fb is the full path to the file/folder referenced in %%b. The equivalent for %%~fa. %%~nxa is the name and extension of the file/folder referenced in %%a.
The code encloses all paths in quotes to avoid problems with spaces, and test for existence of the folder before creation and backup
You can remove the inner for %%b and use the composed target folder in the md and svnadmin commands (as in your code), but this aditional for allows to write only once how the target folder is defined.
for /f "tokens=*" %%G in ('dir /b /a:d "%Source%"') do (
md %destination%\%%G
svnadmin hotcopy %Source%\%%G %Destination%\%%G
)
The problem with the old solution was instead of getting folder names in directory it gets subdirectory of the directory. This is why the result was something like C:\where\the\backup\will\be\C:\where\the\folders\are
That was the reason that I got error:The filename, directory name, or volume label syntax is incorrect. The script part (works fine) that I have shared with you above in answer tokenize the subdirectory to get the folder name and use it.

Delete all files in folder except file in list using batch

I have three file in a folder (temp folder) that are
1.txt
2.exe
3.txt
Now I will use to batch to write script for deleting all files in the folder except one file (3.txt). How to write it in script. I try to use
del temp /Q
But it will delete all file in my folder. I don't want to delete all. I only want to delete 1.txt and 2.exe. Let consider number of file is large.
If you loop through the contents of a directory, you can apply whatever logic you might want, and perform whatever operations you might want on those contents. Example:
#echo off
setlocal enableextensions enabledelayedexpansion
set dirPath=C:\Users\BuvinJ\Desktop\test
pushd !dirPath!
:: Loop through all files in this directory
for %%a in (*) do (
set fileName=%%a
if "!fileName!"=="1.txt" (
echo FOUND: !fileName!
) else (
echo OTHER: !fileName!
)
)
:: Loop through all sub directories in this directory
for /d %%a in (*) do (
set dirName=%%a
echo Directory: !dirName!
)
popd
You might also want to step through the contents "recursively", i.e. drilling down into the nested sub directories. To do so add /r after your for statement (like with the /d in the example to loop through directories instead of files).
For more on this kind of looping, check this out: http://ss64.com/nt/for2.html
You can use the command
except 3.txt del temp /Q
It will delete all files in the folder except 3.txt

Batch file: How to save/use subdir-name when iterating thru directories with FOR /R {directory} in (.fileext)

I'm trying to unrar files with a batchscript. I want to iterate thru a folder and its subfolders and whenever there is a .rar file I want it to rar its content into the same folder as the .rar file is in.
I'm using unrar.exe to do this and it needs me to specify the output folder when raring.
If I dont specify any folder it rars it to the "current folder", which is the folder for the batfile.
this is my code:
FOR /R %dir_of_file% %%X in (*.rar) do (
"%unrarexe_path%\unrar.exe" x -y -r %%X {set unrarfolder path}
)
Is there some way to in evry iteration in the for loop, save the path to the current subdirectory that I'm looking thru?
Something like:
set unrar_to_here={haxcommand that will give me current subdirectory in for-loop}
Hope my question is understandable:)
Does this do what you want? Just make the current working directory the directory containing the rar file and extract to the current folder. I'm not sure about the syntax of the unrar command though. I leave that to you. :)
FOR /R %dir_of_file% %%X in (*.rar) do (
pushd "%%~dpX"
"%unrarexe_path%\unrar.exe" x -y -r %%X
popd
)
Try this. It will put the files in the same folder as the archive:
#echo off &setlocal
FOR /R %dir_of_file% %%X in (*.rar) do (
"%unrarexe_path%\unrar.exe" x -y -r "%%~X" "%%~dpX"
)
endlocal

Resources