I have some software folders like these:
CCleaner 3.17
Firefox v.18.0.2
Avant Browser v2013.01.09
as you can see some of them have the "v" and "v." as version indicator and some does not have it.
I am trying to find a way to add "v." before the number in a folder and all of its subfolders using batch file. if it does not have "v" or "v." add "v." and if it has "v" replace it with "v." and in case if it has the "v." do nothing.
I will be grateful if you could please help me in this matter.
Thanks in Advance
This is a bit tricky. My solution assumes that the version number is the remaining text after the last space in the existing folder name.
The code below will appropriately rename all folders in the current directory that have at least one space in the name. It does not recurse into sub-folders. The code will break if the version text contains ! or =, though that is unlikely to be a problem.
#echo off
setlocal disableDelayedExpansion
for /d %%F in ("* *") do (
set "folder=%%F"
setlocal enableDelayedExpansion
for %%A in ("!folder: =\!") do (
for /f "eol=: tokens=* delims=v." %%B in ("%%~nxA") do (
if "%%~nxA" neq "v.%%~B" ren "!folder!" "!folder:%%~nxA=v.%%~B!"
)
)
endlocal
)
If you want to recurse into the sub-folders, then an outer loop is required, and the folders must be sorted in reverse order so that the deepest folders are processed first.
#echo off
setlocal disableDelayedExpansion
for /f "eol=: delims=" %%D in ('dir /s /ad /b * ^| sort /r') do (
pushd "%%D"
for /d %%F in ("* *") do (
set "folder=%%F"
setlocal enableDelayedExpansion
for %%A in ("!folder: =\!") do (
for /f "eol=: tokens=* delims=v." %%B in ("%%~nxA") do (
if "%%~nxA" neq "v.%%~B" ren "!folder!" "!folder: %%~nxA= v.%%~B!"
)
)
endlocal
)
popd
)
Related
Need a bit of help with this script
for /d %%D in ("*") do (
for %%F in ("%%D\*.jpg") do (
ren "%%~dpF(*).txt" "(*) %%~nF.*"
)
)
This is the original script and this is what it does
Before
filename.jpg
(1).txt
Result
filename.jpg
(1) filename.txt
it copies the filename from the jpg and adds it to the filename of the txt file
what I have been trying to do is two things
I want to add a controlled Sub folder reader to it, and I would like to the filename to be copied between certain points of the txt files
Before
filename.jpg
(1)(name).txt
Result
filename.jpg
(1) filename (name).txt
I have tried like 10 different ways to make this work and for some reason I can't
tried this
FOR /f "delims=" %%q IN ('dir /b /s /a-d "Ready\(*)(Name).txt"') DO call :label "%%q"
goto :eof
:Label
set "FILE=%~1"
for /d %%D in ("*") do (
for %%F in ("%%D\*.jpg") do (
ren "%%~dpF(*)(Name).txt" "(*) %%~nF (*).*"
)
)
and I removed this as well for /d %%D in ("*") do (
and tried this
FOR /f "delims=" %%q IN ('dir /b /s /a-d "Ready\(*)(Name).txt"') DO call :label "%%q"
goto :eof
:Label
set "FILE=%~1"
for %%F in ("*.jpg") do (
ren "%%~dpF%~1" "(*) %%~nF (*).*"
)
and tried this
for /d %%D in ('dir /b /s /a-d "*"') do (
for %%F in ("%%D\*.jpg") do (
ren "%%~dpF(*)(Name).txt" "(*) %%~nF (*).*"
)
)
Any help would be great
Thank you
#ECHO OFF
SETLOCAL
rem The following setting for the source directory is a name which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
PUSHD "%sourcedir%"
for /d %%B in ("*") do (
for %%E in ("%%B\*.jpg") do (
FOR /f "tokens=1,2delims=()" %%q IN ('dir /b /a-d "%%~dpnxB\(*)(*).txt" 2^>nul') DO (
rem files matching "(*)(*).txt only
REN "%%~dpnxB\(%%q)(%%r).txt" "(%%q) %%~nE (%%r).txt"
)
FOR /f "tokens=1*delims=()" %%q IN ('dir /b /a-d "%%~dpnxB\(*).txt" 2^>nul') DO IF /i "%%r" equ ".txt" (
rem files matching "(*).txt only
REN "%%~dpnxB\(%%q).txt" "(%%q) %%~nE.txt"
)
)
)
popd
GOTO :EOF
Caution : This batch is armed. It will rename files. Always verify against a test directory before applying to real data.
The outer loop on %%B gets the directory names. No surprise there.
The next loop on %%E gets the .jpg names. No surprise there.
The first loop on %%q looks at the .txt files that fit the pattern (*)(*).txt and re-assembles the parts as required for the rename.
The second loop on %%q looks at the .txt files that fit the pattern (*).txt which may include the just-renamed files matching (*)(*).txt now (*) jpgfilename (*).txt, so this time, %%r must be .txt to exclude these newly-renamed files.
I'll repeat
Caution : This batch is armed. It will rename files. Always verify against a test directory before applying to real data.
I'm trying to move files into existing sub-folders based on the file names.
For example, I want to move a file named AP16742 found in the directory X:\Files into a folder named AP in the directory X:\Files\AP. Other files named MO14823 I want to move into a folder named MO in the directory X:\Files\MO.
I'm inexperienced in coding, so I need explanations to go with a provided example.
This is what I tried:
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%*.xml" ') DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN ( 'dir /b /ad "%destdir%*%%b*" ' ) DO (
ECHO(MOVE "%%a" "%destdir%\%%d\"
)
)
)
GOTO :EOF
A simple explanation would have been:
you want to distribute files in X:\Files to subfolders with the
first 2 letters of the file name.
To get a substring you need to set the content from for variable into
a normal variable.
Setting and using a variable inside a (code block) requires delayed
expansion.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Pushd "X:\Files"
For %%A in (*) do (
Set "File=%%~nA"
if not exist "!File:~0,2!" md "!File:~0,2!" 2>&1>Nul
Move "%%A" "!File:~0,2!"
)
Popd
In case you want to move only distinct 2 letter pairs and not all files that's also possible without great effort.
A modification of LotPings answer, this uses RoboCopy with its /MOV option, which will create directories as needed and move the files to them:
#Echo Off & SetLocal EnableDelayedExpansion
CD /D "X:\Files" 2>Nul || Exit /B
For %%A In (*) Do (Set "File=%%~nA"
RoboCopy . "!File:~,2!" "%%A" /MOV >Nul)
I am new to batch scripting . I need to delete all files in a folder that DOES NOT contains some word in the file
found this code
#echo off
setlocal
pushd C:\Users\admin\Desktop\bat
findstr /ip /c:"importantWord" *.txt > results.txt
popd
endlocal
So how i can WHITE list this files, and delete all other?
Or i think there is easy way with just check if !contains and delete
but i don`t know how?
Supposedly, this problem could be solved in a very simple way combining these findstr switches: /V that show results when the search string is not found, and /M that show just the name of the files; that is:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
for /F "delims=" %%a in ('findstr /ipvm /c:"importantWord" *.txt') do del "%%a"
Unfortunately, the combination of /V and /M switches don't properly work: the result of /V is based on lines (not files), so a modification in the method is needed:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
rem Create an array with all files
for %%a in (*.txt) do set "file[%%a]=1"
rem Remove files to preserve from the array
for /F "delims=" %%a in ('findstr /ipm /c:"importantWord" *.txt') do set "file[%%a]="
rem Delete remaining files
for /F "tokens=2 delims=[]" %%a in ('set file[') do del "%%a"
This method is efficient, particularly with big files, because findstr command report just the name of the files and stop searching after the first string match.
#echo off
setlocal
set "targetdir=C:\Users\admin\Desktop\bat"
pushd %targetdir%
for /f "delims=" %%a in ('dir /b /a-d *.txt') do (
findstr /i /p /v /c:"importantWord" "%%a" >nul
if not errorlevel 1 echo del "%%a"
)
popd
endlocal
Not really sure what you want to do with /pfiles - files containing non-ansi characters appear to return errorlevel 1for these. if not errorlevel 1 will echo the files that do not contain the required string - remove the echo to actually delete the file(s)
This should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "pathToFolder=C:\FolderToEmpty"
SET "wordToSearch=ImportantWord"
FOR /F "tokens=*" %%F IN ('dir %pathToFolder% /b *.txt') DO (
findstr /IP %wordToSearch% "%pathToFolder%\%%F">nul
IF !ERRORLEVEL!==1 (
DEL /Q "%pathToFolder%\%%F"
)
)
You will have to set the proper path to the folder you want to delete the files from and to replace ImportantWord with the substring you are looking for.
I need to delete duplicate folder names from a folder list. The duplicates occur when there's more than 1 subfolder. I end up with a list like below. I want to get rid of any line that has a sub2 folder.
folder1\sub1
folder2\sub1
folder2\sub1\sub2
folder3\sub1
Following code works if there is only one sub2 foldername, but it's awkward--hopeless if more than one sub2. There's gotta be a better way. Any help much appreciated.
#Echo off
SETLOCAL EnableDelayedExpansion
:: Write the sub2 foldernames to a tmp file
For /f "tokens=3 delims=\" %%I IN (folderlist.txt) DO Echo %%I >>temp.tmp
:: Set var for each sub2 name in tmp file and
:: call routine to write lines that don't contain that name
For /f %%G in (temp.tmp) do (
Set findstring=%%G
CALL :FindDup
)
EXIT
:findDup
For /f %%H in ('Type folderlist.txt ^|Find "!findstring!" /v') Do (
Echo %%H >> NoDup.txt
)
exit /b
FWIW: I'm using this command to generate the list, then deleting the path preceding folder1, folder2, etc
For /d %%G in (*) do dir /ad /on /s /b "%%G" >> folderlist.txt
you are almost there, if you just want the resulting list after eliminating the subfolders, just try to echo the appropiate lines to the list file, having copied it first into a temporary.
move folderlist.txt %temp%\folders.txt
for /f "tokens=1,2,* delims=\" %%a in (%temp%\folders.txt) do (
if .%%c==. echo %%a\%%b >>folderlist.txt
)
if you want to remove the folder from the disk, then change the line to
if not .%%c==. rd /s %%a\%%b\%%c
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN (q20840158.txt) DO (
FOR /f "tokens=1,3delims=\" %%b IN ("%%a") DO IF "%%c"=="" ECHO(%%a
)
GOTO :EOF
I used a file named q20840158.txt containing your data for my testing.
But - It's really unclear what you mean by a "duplicate". How precisely do you define a duplicate in this context?
#ECHO OFF
SETLOCAL
COPY NUL newfile.txt >NUL 2>nul
FOR /f "delims=" %%a IN (q20840158.txt) DO (
ECHO %%a|FINDSTR /I /L /g:"newfile.txt" >NUL 2>NUL
IF ERRORLEVEL 1 >>"newfile.txt" ECHO(%%a
)
TYPE newfile.txt
GOTO :EOF
I used a file named q20840158.txt containing your data for my testing.
Produces newfile.txt
Ah- you want to make sure that any additions to the list don't contain a previous entry...
I have a large set files with names structured string_int_int_int_string.extension, and would like to batch rename them with left zero padding to 7 digits on the second int.
Example: rename stringA_1_2_3_stringB.jpg to stringA_1_0000002_3_stringB.jpg.
I've seen some helpful posts here, here, and here but haven't quite done it.
Here is what I have so far (not working, of course):
dir /b *.* >temp.txt
for /f "tokens=%%1,%%2,%%3,%%4,%%5 delims=_" %x in (temp.txt) do (
setlocal enabledelayedexpansion
set PAD=000000%%k
set PAD=!PAD:~7!
ren "%%i_%%j_%%k_%%l_%%m" %%i_%%j_%PAD%_%%l_%%m
)
I specifically want to do this with a batch file, not some other language or tool. (I'm aware of the various renaming tools out there.)
Any help is most welcome!
setlocal EnableDelayedExpansion
dir /b *.* >temp.txt
for /F "tokens=1-5 delims=_" %%a in (temp.txt) do (
set PAD=000000%%c
set PAD=!PAD:~-7!
ren "%%a_%%b_%%c_%%d_%%e" "%%a_%%b_!PAD!_%%d_%%e"
)
I use FINDSTR to filter out file names that don't match the specified pattern. A total of 4 tokens are needed - the first 3, followed by the rest of the file name.
#echo off
setlocal disableDelayedExpansion
for /f "tokens=1,2,3* delims=_" %%A in (
'dir /b /a-d * ^|findstr /r "^[^_]*_[0-9]*_[0-9]*_[0-9]*_[^_]"'
) do (
set "mid=%%C"
set "pad=0000000%%C"
set "start=%%A_%%B"
set "end=%%D"
setlocal enableDelayedExpansion
echo ren "!start!_!mid!_!end!" "!start!_!pad:~-7!_!end!"
endlocal
)