Hi I'm trying to make a batch file that digs thru a directory and checks if all the files in its sub-directories are the same as in another directory (with the same name and same sub-directory names)
ex:
C:/Users/Administrator/desktop/directory1/global/config/project.config
C:/directory2/global/config/project.config
I'm able to fetch all the files that need to be compared with a for loop
The echo %%f gives me a path like:
C:/Users/Administrator/directory1/global/config/project.config
But I want to remove the beginning and store it in a new variable so I can easily compare files with an FC command. I don't know how to correctly do a string substitution when in a for loop. I want to get just this:
/global/config/project.config
my code for now
set b="c:\Users\Administrator\desktop\"
set j=""
for /f %%f in ('dir /a:-d /s /b /r c:\Users\Administrator\desktop\global') do (
set c=%%f
%c:b=j%
echo %%c
)
#echo off
set "dir1=c:\smth"
set "dir2=c:\dir\smth2"
setlocal enableDelyaedExpansion
for /f %%F in ('dir /a:-d /s /b /r "%dir1%"') do (
set "file_path=%%F"
if not exist "!file_path:%dir1%=%dir2%!" (
echo file %%F does not exists in %dir2%
)
)
endlocal
The better way to use a call function
setlocal enableDelyaedExpansion
for /f %%F in ('dir /a:-d /s /b /r "%dir1%"') do call :foo %%F
:foo
set "file_path=%1"
if not exist "!file_path:%dir1%=%dir2%!" (
echo file %1 does not exists in %dir2%
)
Related
I have went through a lot of guides for it, but havent found a way to do something similar to lets say turn all files in folder, like these files:
djhwu4s_cat_ruhg29.png
397y_dog_j0929_ej93.png
8yhh_owl.png
into these:
_cat.png
_dog.png
_owl.png
So basically removing everything from file names but a list of predefined strings i am searching for. In example above i would define list as "_cat", "_dog", "_owl". I know that each file will have only one of these variables, and there will be only one file with each of them in folder.
Will appreciate any tips on how to achieve that. Thanks in advance!
edit:
Here is what i came up with (with stuff i can understund) and what seems to be working fine now.
#echo off
setlocal enabledelayedexpansion
set v1=_cat-cat
set v2=_cat-owl
set v3=_cat
set v4=_dog
set v5=_owl
set v6=_horse
FOR /L %%a IN (1,1,6) DO (
rem echo %%a
rem echo !v%%a!
FOR /f %%f in ('dir /b /a:-D *!v%%a!.*') DO (
REN %%f !v%%a!.*
)
FOR /f %%f in ('dir /b /a:-D *!v%%a!_*.*') DO (
REN %%f !v%%a!.*
)
)
rem using two passes of this simpler code i can grasp and understund with dot and with underscore
rem after constructed variables value i make sure cat-cat is not recognised as and renamed to cat
rem no matter if im getting file with that variable as the last string before extension or another underscore
rem Gonna test it in combat now
For some reason this stuff doesnt work with files containing spaces and characters like:
"ab’c efg_dog.png"
FOR /L %%a IN (1,1,36) DO (
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
REN "%%f" "!v%%a!.*"
)
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
REN "%%f" "!v%%a!.*"
)
)
After further testing i have realised the problem starts with the %%f, and not the REN function as i thought. echo %%f before ren gives just the first part of the name to the first space, hence the REN function cant find the file. In case of "ab’c efg_dog.png" after finding the file with dir, the %%f becomes just "ab’c".
edit: After more tests and experiments and adding those "delims" to the code, the echo now shows proper, full names to be renamed, but it replaces that weird ’ character with ' for the REN command and thats why it still cant find the file to rename.
FOR /L %%a IN (1,1,36) DO (
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
)
#ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes 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 /f "delims=" %%e IN ('dir /b /a-d "*_*" 2^nul ^|findstr /v /b "_"') DO (
FOR /f "tokens=2delims=_." %%y IN ("%%e") DO ECHO REN "%%e" "_%%y%%~xe"
)
POPD
GOTO :EOF
For lack of examples, here's a start.
Always verify against a test directory before applying to real data.
Process the list of filenames that match *_*; find those names that do not start _, pick the second string between the delimiters _ and . and rename using that string (in %%y), prefixed by _ and appended with the original extension.
The ren command is simply echoed to the screen for verification. When happy, remove the echo before the ren to actually execute the rename.
I have a .txt file with a list of files that are on a network drive (M:). I need a batch file to go through that list, search for the files which are in sub-directories of a single folder on that drive, and then copy them to another folder. I have tried quite a few solutions with no luck.
the text file is a list of files and extensions i.e.
abc.step
afer.iges
ca76dc7d.sldprt
Here's what I tried so far
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
cls
set dest=C:\Users\kduquette.000\Desktop\Files
for /f "TOKENS=*" %%f in (C:Desktop\Files\list.txt) do (
set i=1
for /f "tokens=*" %%F IN ('dir M: "%%~f"') Do (
for %%N in ("%%F") Do (
set name=%%~NN
set ext=%%~XN
)
copy "%%~F" "%dest%\!name!_!i!!ext!"
set /A i=!i!+1
)
)
ENDLOCAL
We can just do a check if file exists and copy it:
#echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do if exist "M:\Cads\%%i" echo copy "M:\Cads\%%i" "%dest%"
Note, for now, I added echo to the copy string, so you can test the result first. Once happy with the results, remove echo
If however you want to search for the files, recursively throughout M:\ drive then:
#echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do (
pushd M:\
for /f "delims=" %%a in ('dir /b /s "%%i"') do echo copy "%%~fa" "%dest%"
popd
)
I need to use a bat named here.bat to process files in the same directories. Here is an example:
mydir
|----mybat.bat
|----here.bat
|----subdir
|-----subdir2
|-----subdir3
|-----subdir31
| |-----a.jpg
|
|-----subdir32
|-----b.jpg
I need to use here.bat to process a.jpg and b.jpg. So I write a bat file named mybat.bat as below:
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
For /d %%A in (.\subdir\*) Do (
Set "Files="
For /F "delims=" %%B in ('dir /S /B "%%~fA\*.jpg"') Do Set Files=!Files! "%%~fB"
If defined Files echo call here.bat !Files!
)
pause
When I execute mybat.bat, I get the result as below:
call here.bat "C:\Users\me\mydir\subdir\subdir2\subdir3\subdire31\a.jpg" "C:\Users\me\mydir\subdir\subdir2\subdir3\sudbir32\b.jpg"
But this is not what I need. I need to process jpg files which are in the same directory with here.bat, if they are not in the same directory, they should be processed separately. Meaning that I need the result as below:
call here.bat "C:\Users\me\mydir\subdir\subdir2\subdir3\subdire31\a.jpg"
call here.bat "C:\Users\me\mydir\subdir\subdir2\subdir3\sudbir32\b.jpg"
Your changing requirements make it difficult to provide a solution in the first run. This version should do:
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
for /R ".\subdir" %%A in (.) do (
Set "Files="
If Exist "%%~fA\*.jpg" For /F "delims=" %%B in (
'dir /B "%%~fA\*.jpg"') Do Set Files=!Files! "%%~fB"
If defined Files echo call here.bat !Files!
)
pause
I want to traverse all files within a specific directory and all its subdirectories and then print out the folder name of each file.
I don't know how to get the folder name of each file.
FOR /F "delims=" %%x IN ('dir /B /A /S *') DO (
:: Suppose %%x is 'C:\myfolder\a.txt', the desired output is 'myfolder'
:: %%~nx is not correct
echo ???
)
If you want just the path (without drive, without filename), %%~px is what you need
If you want just the last folder, not the complete path. This is indeed not that trivial:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%x in ('dir /b /a /s *') do (
set "line=%%~dpx"
for /f "delims=" %%y in ("!line:\=.!") do set folder=%%~xy
echo %%~nxx is in: !folder:~1!
)
I think this is what you're looking for:
#echo off
FOR /F "delims=" %%F IN ('dir /B /A /S *') DO (
for %%D in ("%%~dpF\.") do echo %%~nxD
)
pause
I need to write a batch file to convert every file in every subfolder. I came up to the following solution:
set INDIR=<some path>
set OUTDIR=<some path>
mkdir "%OUTDIR%"
for /f "tokens=1*delims=." %%f in ('dir %INDIR% /b /a-d') do (
rem %%f is file name
rem %%g is extension
call convert_one . %%f %%g
)
)
for /f "delims==" %%d in ('dir %INDIR% /ad /b') do (
rem %%d is relative path
mkdir %OUTDIR%\%%d
for /f "tokens=1*delims=." %%f in ('dir %INDIR%\%%d /b /a-d') do (
rem %%f is file name
rem %%g is extension
call convert_one %%d %%f %%g
)
)
The problem is that it iterates through the first level subfolders only. When I add /s key to the dir command it returns full pathes instead of relative.
How can I improve the script to process all levels subfolders?
PS: I need separate values for relative path, file name and extension.
You don't show us convert_one - but here's a few clues...
for /f "delims=" %%f in ('dir %INDIR% /b /s /a-d') do (
rem %%~dpnf is file name and path
rem %%~dpf is file absolute path
rem %%~nf is file name-part
rem %%~xf is extension
call convert_one "%%~dpf" "%%nf" %%~xf
)
)
See for /?|more from the prompt for more...
(within your subroutine, %~n for n=1..9 strips quotes if needed - applying quotes means that "strings containing spaces" are regarded as a single string.
- maybe make your destination directory there...?)
#echo off
setlocal enableextensions disabledelayedexpansion
set "INDIR=%cd%"
set "OUTDIR=..\out"
subst :: /d >nul 2>&1 & subst :: "%INDIR%"
for /r "::\." %%a in (*) do (
if not exist "%OUTDIR%%%~pa" md "%OUTDIR%%%~pa"
call convert_one ".%%~pa" "%%~na" "%%~xa"
)
subst :: /d
This operates creating a subst drive, so the root of the drive point to the in folder. Then iterates over the folder structure recreating the structure in the output folder and calling the subroutine with the indicated parameters