I need to get the relative filepath of files with bat.
The folder structure
file.bat
- folder1
-- subfolder1
----- abc.image
----- def.image
-- subfolder2
---- more.image
- folder2
-- subfolder1
---- alsoimages.image
-- subfolder2
For abc.image, I'd like to get the name of the folder (here e.g. folder1) and the combination folder+subfolder (here e.g. folder1/subfolder1)
I looked at this post: batch programming - get relative path of file
but I cant make it work for me. The output keeps giving me too many hierachies.
What I want to do ultimately is this:
set "prefix=td"
set "file=list_of_images.xml"
echo ^<?xml version="1.0" encoding="UTF-8" standalone="yes"?^> > %file%
echo ^<files^> >> %file%
for /r %%x in (*.image) do (
echo ^<file folder="(here e.g. folder1)" subfolder="(here e.g. folder1/subfolder1)" id="%prefix%%%~nx" type="image" /^> >> %file%
)
echo ^</files^> >> %file%
Thanks for help and tips!
for /f "tokens=1,* delims=\" %%a in ('
xcopy ".\*.image" "%temp%" /s /e /l
') do if not "%%b"=="" echo(%%b
The easiest way to get a list of files with relative paths is to use an xcopy command to generate the list (/l) of files.
You can also use a subst command to create a "virtual" drive letter with the root of the drive pointing to the required starting folder and then use your same code referencing this drive, that now will not retrieve the upper folder structure
edited to adapt to comments
for /f "tokens=2-4 delims=\" %%a in ('
xcopy ".\*.image" "%temp%" /s /e /l
') do echo folder=%%a subfolder=%%b file=%%c
The output from xcopy command is in the format
.\folder\subfolder\file
so, using backslash as delimiter we skip the first token and retrieve the second (folder), third (subfolder) and fourth (file)
One of next code snippets could help:
for /r %%x in (*.image) do (
for /F "tokens=1* delims=\" %%G in ("%%~px") do (
echo "%%~G" "%%~H" "%prefix%%%~nx"
)
)
Splitting to more than one subfolders:
for /r %%x in (*.image) do (
for /F "tokens=1,2* delims=\" %%G in ("%%~px") do (
echo "%%~G" "%%~H" %%~I "%prefix%%%~nx"
)
)
And so on...
Related
So I'm trying to make my own dir command for cmd. So far it is working great, except I want to output the directories and files sorted by file extension, in the way
dir /o:ge
would display (folders first, then files sorted by file extension).
So far, my code looks like this
#echo off
rem Title
echo.
echo CURRENT DIRECTORY [%cd%]
echo.
rem Directories
for /d %%D in (*) do (
echo [DIR] %%~nD
)
rem Files
for %%F in (*) do (
echo %%~nxF
)
#echo on
This produces:
I'm not sure how to approach outputting the files sorted by file extension. I have searched the web and can't find a solution to this problem. I do realize batch script is very limited, but I still want to try and implement this. I have thought of using a for loop and storing all the file extensions into an "array" (if that exists in batch), and then outputting them by
*.fileExtension
Any suggestions?
Cheers,
Derek
As in my comment…
#Echo Off
Echo CURRENT DIRECTORY [%__CD__:~,-1%]&Echo(
For /F "EOL= Tokens=* Delims= " %%A In ('Dir /B/AD/ON') Do Echo [DIR] %%A
For /F "EOL= Tokens=* Delims= " %%A In ('Dir /B/A-D/OE') Do Echo %%A
Echo(&Pause>Nul
Alternatively…
#Echo Off
Echo CURRENT DIRECTORY [%__CD__:~,-1%]&Echo(
For /F "EOL= Delims=" %%A In ('Dir /OGE/-C'
) Do For /F "Tokens=3*" %%B In ("%%A"
) Do If "%%B"=="<DIR>" (If Not "%%C"=="." If Not "%%C"==".." Echo [DIR] %%C
) Else Echo %%C
Echo(&Pause>Nul
I have a directory structure that reads like:
nnnnnn~substring
Where n are numbers, and substring are letters.
I am trying to write a batch file that tests if a particular file exists inside the directory, and if it does, it should rename the directory to substring.
The batch file should look like:
for /f "tokens=\*" %%a in ('dir /b') do if exist filename (rename nnnnnn~substring substring)
How do I trim all the numbers and the ~ character of the directory name so I can rename it using only the final part of the name?
The numbers before the ~ separator have different lengths, so does the substring after it.
#echo off
setlocal enableextensions disabledelayedexpansion
rem Change to the target folder
pushd "x:\somewhere" && (
rem For each folder inside it matching the indicated pattern
rem Uses a dir command to search only folders and a
rem findstr filter to ensure only matching folders
for /f "delims=" %%a in ('
dir /ad /b *~* ^| findstr /r /c:"^[0-9][0-9]*~..*$"
') do (
rem Check if the folder contains the file
if exist "%%~fa\flagFile.txt" (
rem Split the folder name using the ~ as delimiter
for /f "tokens=1,* delims=~" %%b in ("%%~na") do (
rem Check that the new folder name does not exist
if not exist "%%~c%%~xa" (
rem Execute the rename operation
echo ren "%%~fa" "%%~c%%~xa"
)
)
)
)
rem Restore previous active directory
popd
)
Rename operations are only echoed to console. If the output is correct, remove the echo that prefixes ren command
This may work - test it in a copy of your folder structure:
It assumes that substring in your question does not contain any ~ characters.
#echo off
:loop
for /d /r "d:\base\folder" %%a in (*~*) do (
if exist "%%a\filename" for /f "tokens=1,* delims=~" %%b in ("%%~nxa") do (
ren "%%a" "%%c"
goto :loop
)
)
pause
This is designed for only the folders within the current directory.
#echo off
for /d %%a in (*~*) do (
if exist "%%a\filename" for /f "tokens=1,* delims=~" %%b in ("%%~nxa") do ren "%%a" "%%c"
)
pause
I have 1000's of files like TOP_QUERIES-abc.com.au.csv,TOP_QUERIES-www.abc.com.au.csv, TOP_QUERIES-m.lmn.com.au.csv, TOP_QUERIES-blog.com.au.csvand get-files.php
Is it possible to delete all the .csv files from a folder except for files starting with blog. ,m. , www, and .php from the folder?
I know its possible in php but how can I achieve in batch file?
#ECHO OFF
SETLOCAL
SET "targetdir=U:\destdir"
SET "exclude=blog. m. www."
FOR /f "delims=" %%a IN (
'dir /b /a-d "%targetdir%\*.csv" '
) DO (
ECHO %%a | findstr /B /L "%exclude%" >NUL
IF ERRORLEVEL 1 ECHO(DEL "%targetdir%\%%a"
)
GOTO :EOF
This should get the task done for you. You would need to change the setting of targetdir to suit your circumstances.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.
The .php files are excluded because they don't match the *.csv filename - or did you want to not-delete files starting .php? If so, simply add .php into the exclude variable...
Revision for efficiency:
#ECHO OFF
SETLOCAL
SET "targetdir=U:\destdir"
SET "exclude=blog. m. www."
FOR /f "delims=" %%a IN (
'dir /b /a-d "%targetdir%\*.csv" ^| findstr /B /L "%exclude%"'
) DO (ECHO(DEL "%targetdir%\%%a"
)
GOTO :EOF
I have a need for a batch file or utility that would be able to find any "un-compressed archive folders" that are no longer needed and can now be deleted because the original archive file is still present.
The key is that the "un-compressed folder" and the "original archive file" always have the same name except for the file extension of the archive file. I do not want to automatically delete anything; I just want to create a list of folders that I can manually check out. So the sequence would be a 4 step process:
1) Search for all archive files using wildcards such as *.zip, *.rar, *.iso
2) Create a list of all of the filenames that are found - minus the file extensions
3) Use the list created in step two to search for any folders with those names
4) Create a text file with any folders found in step three.
I tried modifying a batch file that I found in these posts but it didn't work and I would like to start from scratch. Any help would be greatly appreciated.
Thanks
Ok, I'll do this step by step:
Step 1:
set dir="C:\...[path to root directory]"
where /r %dir% *.zip *.iso *.rar >> log.txt
Note the where utility should be on your computer if using windows 7.
Step 2:
ren log.txt log.tmp
for /f "delims=." %%a in (log.tmp) do (Echo %%a >> log.txt)
del log.tmp
The above code will not handle files names with periods in it
Step 3:
for /f "tokens=*" %%a in (log.txt) do (
where /r %dir% %%a* >> files.txt
)
Not 100% sure if above will work, tell me if it doesn't.
Step 4:
Rem This code will handle file paths to directories
Ren files.txt files.tmp
for /f "tokens=*" %%a in (files.tmp) do (
Echo %%~pa >> files.txt
)
del files.tmp
Rem The below code will ged rid of repeated direcotries
ren files.txt files.tmp
Echo. > files.txt
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (files.tmp) do (
set var=1
for /f "tokens=*" %%b in (files.txt) do (
if "%%~a" equ "%%~b" set var=0
)
if !var!==1 Echo %%a >> files.txt
)
del files.tmp
And I'm rather confident that should work. Of course I haven't tested this, but run all of this with #Echo on and a pause command between each sect (or as seperate batch files) so that if an eror does occur I can try helping you.
Hope this was helpful, Mona.
#echo off
setlocal enableextensions
set "rootDir=d:\_data_"
set "fileList=%~dp0\%~n0.files.list"
set "folderList=%~dp0\%~n0.folders.list"
rem generate list of compressed files names
break > "%fileList%"
for /F "tokens=*" %%f in ('where /r "%rootDir%" *.zip *.rar *.iso *.7z 2^>nul') do (
>> "%fileList%" echo %%~nf
)
rem check compressed file list against directory list
break > "%folderList%"
for /F "tokens=*" %%f in ('dir "%rootDir%" /s /b /ad ^| findstr /e /g:"%fileList%" ') do (
>> "%folderList%" echo %%f
)
type "%folderList%"
endlocal
I have the folder and file structure as given below. I am in need of a MS DOS batch file to rename the files in multiple folders. Can anyone please help out?
- Main Folder
-->Sub Folder1
--- File1_EN.txt
--- File2_EN.txt
--> Sub Folder2
--- File3_EN.txt
--- File4_EN.txt
I want to rename the suffix "EN" in file names to "ENU".
#echo off
for /D %%d in (*) do (
ren "%%d\File*_EN.txt" "File*_ENU.txt"
)
You can do it by this way:
#Echo OFF
Set "Folder=C:\Users\Administrador\Desktop\Nueva carpeta"
Set "Suffix=_EN"
Set "Replace=_ENU"
Set "RegEx=\".*%Suffix%\"$"
FOR /R "%Folder%" %%# in ("*") DO (
(Echo "%%~n#"| FINDSTR /I "%RegEx%" 1>NUL) && (
Set "NewFileName=%%~nx#"
Call Set "NewFileName=%%NewFileName:%Suffix%=%Replace%%%"
Call Echo [+] Renaming: "%%~nx#" "%%NewFileName%%"
Ren "%%#" "%%NewFileName%%"
)
)
Pause&Exit
The Findstr is to ensure the matched string is a suffix, is better than doing a substring or splitting the filename from "_" character to the right.
Try this:
ren folder1\file*.txt file*_enu.txt
ren folder2\file*.txt file*_enu.txt
If you want all child folders to be changed use:
for /f "delims=*" %a in ('dir File*_EN.txt /b /s') do ren "%a" File*_ENU.txt