After researching how to move files w certain words in their title recursively...and assuming this is the result ( I have the code in a win11 .bat file which sits in the root of d:\ )
for /r %%a IN (*apples* *oranges*) do (
move /y "%%a" "d:\fruit" )
Is there a way I can EXCLUDE a specific directory from the search? The directory "d:\bananas" for example?
Thanks!
Try
for /r %%a IN (*apples* *oranges*) do (
echo "%%a"|find /i "d:\bananas\" >nul
if errorlevel 1 move /y "%%a" "d:\fruit" )
The find sets errorlevel to 1 if the string is not found.
This run faster because it does not execute the 17 Kb find.exe command on each file:
setlocal EnableDelayedExpansion
set "exclude=d:\bananas"
for /r %%a IN (*apples* *oranges*) do (
if "!exclude:%%~DPa=!" equ "%exclude%" move /y "%%a" "d:\fruit"
)
The "!exclude:d:\bananas\=!" part try to delete the path of each file from the variable. If the result is the same, the path is not in the variable...
You can also do a partial search of the beginning of the path of each file this way:
for /r %%a IN (*apples* *oranges*) do (
set "filepath=%%~DPa"
if "!filepath:d:\bananas=!" equ "!filepath!" move /y "%%a" "d:\fruit"
)
Related
so I have a bunch of folders with similar directories to the below:
.\page\1\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\2\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\3\randomStringofCharacters\randomStringofCharactersAgain.png
I want to rename all the .png files the number just before the first randomStringofCharacters. So basically
.\page\1\randomStringofCharacters\randomStringofCharactersAgain.png -> 1.png
.\page\2\randomStringofCharacters\randomStringofCharactersAgain.png -> 2.png
.\page\3\randomStringofCharacters\randomStringofCharactersAgain.png -> 3.png
Is there any batch script that can do this? I have tried:
#Echo OFF
FOR /D /R %%# in (*) DO (
PUSHD "%%#"
FOR %%# in ("*.png") DO (
Echo Ren: ".\%%~n#\%%#" "%%~n#%%~x#"
Ren "%%#" "%%~n#%%~x#"
)
POPD
)
Pause&Exit
Yet this only renames the file with the parent directory.
And if possible is there a way to move all such renamed .png files in to a newly made folder in .\page\ (the same place where the .bat is) with the folder name of page?
Thanks in advance!
Could be something like this (you have to drag and drop the main folder to the batch):
#echo off
if exist "%~1" (IF not exist "%~1\" exit) else (exit)
if /i not exist "%~dp0Page" md "%~dp0Page"
pushd "%~1"
for /f "delims=" %%a in ('dir /s /b *.png') do Call :Rename "%%~a" "%%~dpa"
exit
:Rename
set Cpath=%~2
set Cpath=%Cpath:~0,-1%
For %%a in ("%Cpath%") do set Cpath=%%~dpa
set Cpath=%Cpath:~0,-1%
for %%a in ("%Cpath%") do set NName=%%~nxa
move "%~1" "%~dp0Page\%NName%%~x1"
goto :EOF
You should not use for /R to handle items in a predefined directory hierarchy depth:
#echo off
rem // Iterate through the numeric target sub-directories (`1`, `2`, `3`, etc.):
for /D %%K in (".\page\*") do (
rem // Iterate through sub-sub-directories `randomStringofCharacters`:
for /D %%J in ("%%~K\*") do (
rem // Iterate through `.png` files:
for %%I in ("%%~J\*.png") do (
rem // Actually rename the current file:
ren "%%~I" "%%~nxK%%~xI"
)
)
)
Note, that this approach does not check whether there is only one .png file at each location.
i'm looking for a way on Windows to find all files with a specific file extension length and copy them to another location while preserving the folder structure.
For example lets say that I want to copy all files on my D: drive, with a file extension length of excatly six (*.******) and copy them to another location while also keeping the folder structure.
Is this possible in CMD?.
C:\Users\PeterR>copy C:\Users\PeterR\Documents\cmd\???.txt C:\Users\PeterR\Documents\cmd1\
--use ? to specify the lentgh. for six characters it's ??????.extension
What about the following code snippet:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_SOURCE=D:\"
set "_DESTIN=E:\"
pushd "%_SOURCE%" || exit /B 1
for /F "delims=" %%F in ('
xcopy /L /E /I ".\*.*" "E:\" ^| find ".\"
') do (
for /R "D:\" %%F in ("*.*") do (
set "EXT=%%~xF"
setlocal EnableDelayedExpansion
if "!EXT:~6!"=="" if not "!EXT!"=="!EXT:~5!" (
endlocal
move /Y "%%~F" "%_DESTIN%\%%~F"
) else endlocal
)
popd
endlocal
exit /B
Situation:
I try to move files inside a loop in shell but my code is not working.
for /D %%F in (*) do (
if "%%F" NEQ "%directoryToPutFilesIn%" (
move /y "%%F" "%directoryToPutFilesIn%"
)
)
After hours of testing it, I realized it's because %%F is pointing to the folder, hence the file cannot be moved.
Bad solution:
The way I made it work and confirmed my suspicions is by saving the value of %%F in another variable and using that variable on next turn to move the file. Note, what is following needs an initialisation of %precedentFile% for the first turn.
for /D %%F in (*) do (
move /y "%precedentFile%" "%directoryToPutFilesIn%"
if "%%F" NEQ "%directoryToPutFilesIn%" (
move /y "%%F" "%directoryToPutFilesIn%"
set precedentFile=%%F
)
Problem:
This solution is not practical and feels wrongs. Is there a way to adapt my current code to do it, or simply another way?
Try below code to move files from one folder to another in batch script:
for /f %%a in ('dir /a:-D /b') do move /Y "%%~fa" "%directoryToPutFilesIn%"
Explanation :
dir /a:-D /b : This command will list all files in the directory
move /Y "%%~fa" "%directoryToPutFilesIn%" : This will move all files in the directory where this command is executed to the destination you have mentioned.
%%~fa : This command will get full qualified path of the file with it's name.
Try Below code Move directories :
Below command will move directories in the Path where this command is executed to the destination provided. In this that will be H:\ Drive, Change it accordingly
for /D %%b in (*) do move /Y "%%~fb" "H:\"
After successfully removing a bunch of Google Drive Folder duplicates, some files retain a "filename(2)"name.
Is there a way to batch rename every file so the parenthesis and the number inside the parenthesis is gone?
That includes folders and sub-folders.
Try like this :
Create a file test.bat with the code below in it and replace the path to test in the var $path
#echo off
set $path="C:\Users\CN Micros\Desktop\PROGRAMMATION\test"
for /f "tokens=1-3 delims=^(^)" %%a in ('dir /b/a-d %$path%') do (
if exist %$path%\"%%a(%%b)%%c" echo ren %$path%\"%%a(%%b)%%c" "%%a%%c"
)
pause
Then run it in the CMD or by double clicking.
If the output is ok for you remove the echo
The program create 3 tokens : %%a = what's before the (), %%b What's inside the () and %%c what's after the ().
Then we arrange this 3 tokens to rename the files without the ().
If you have some file who have the same final name ie : "file(1)name", "file(2)name" --> "filename"
It will work only with the first one. If you have this case you have to add a counter at the end of file to be sure that they will be renamed.
This will create renfiles.bat.txt for you to examine in Notepad and then rename to .bat and execute if you are happy with it.
#echo off
dir /b /a-d *(*).* |find /i /v "%~nx0" |find /i /v "repl.bat" |repl "(.*)\(.*\)(\..*)" "ren \q$&\q \q$1$2\q" xa >"renfiles.bat.txt"
This uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
Edit: This version will recurse through subdirectories:
#echo off
dir /b /s /a-d *(*).* |find /i /v "%~nx0" |find /i /v "repl.bat" |repl ".*\\(.*)\(.*\)(\..*)" "ren \q$&\q \q$1$2\q" xa >"renfiles.bat.txt"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%sourcedir%\*" '
) DO (
SET "name=%%~na"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "newname=!name:)=!"
SET "newname=!newname:(=!"
IF "!name!" neq "!newname!" (
IF EXIST "%%~dpa!newname!%%~xa" (ECHO cannot RENAME %%a
) ELSE (ECHO(REN "%%a" "!newname!%%~xa")
)
endlocal
)
GOTO :EOF
You'd need to set your required directory into sourcedir. I used u:\sourcedir which suits my testing.
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.
I would like to have a batch file wich will sort files by file type and sorts them into folder.
For example I will run this batch file in some folder and .PDF files will be saved in "PDF" folder, same to do with other file types. Is possible to do that in command line?
Thank you.
Please put the code below in a .bat file and save it to your folder with files and run it.
#echo off
rem For each file in your folder
for %%a in (".\*") do (
rem check if the file has an extension and if it is not our script
if "%%~xa" NEQ "" if "%%~dpnxa" NEQ "%~dpnx0" (
rem check if extension forlder exists, if not it is created
if not exist "%%~xa" mkdir "%%~xa"
rem Copy (or change to move) the file to directory
copy "%%a" "%%~dpa%%~xa\"
)
)
Try this:
#echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set "fol=%%~xa" & set "fol=!fol:.=!"
if not exist !fol! md !fol!
for /f %%b in ('dir /on /b *.*') do (
if %%~xb EQU .!fol! move %%~nxb !fol!
)
)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET destdir=c:\destdir
SET "extdone=:"
FOR /f "delims=" %%a IN ('dir /b /a-d') DO (
SET ext=%%~xa
IF DEFINED ext (
SET extdone|FIND /i ":%%~xa:" >NUL
IF ERRORLEVEL 1 (
SET extdone=:%%~xa: !extdone!
IF EXIST "%destdir%\!ext:~1!" (
ECHO MOVE "*%%~xa" "%destdir%\!ext:~1!"
) ELSE (ECHO no MOVE "%%~xa")
)
)
)
GOTO :EOF
This batch should do as you ask.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files.
I've assumed that you only wish to move the files if destinationdirectory\extensionfound exists. If you want to create a directory for a new extension, simply add a line
md "%destdir%\!ext:~1!" 2>nul
after the SET extdone=:%%~xa: !extdone! line.