I have a directory of folders that I need to strip a variable number of characters from the beginning of the folder name but leave the second part of the folder intact. Every folder has the same delimiter "..." to differentiate between the information that I don't need and the information that I do need.
Example:
C:\Users\Testing
Current folder structure:
C:\Users\Testing\Folder 1...ABC
C:\Users\Testing\Folder 2...ABD
C:\Users\Testing\New Folder 3...ABE
C:\Users\Testing\FodlersForDays...DBA
Adjusted Folder Structure
C:\Users\Testing\ABC
C:\Users\Testing\ABD
C:\Users\Testing\ABE
C:\Users\Testing\DBA
I need a batch script but the only thing I have been able to come up with just removes everything after the delimiter. The information after the delimiter is unique and will not cause any duplicated folder names, but I don't know how to keep the information that I need and remove what's not necessary.
#echo off
set "SourceDir=C:\Users\Testing"
FOR /d %%i IN ("%SourceDir%\*") DO FOR /f "delims=..." %%j IN ("%%i") DO REN "%%~i" "%%~nj"
set SourceDir=
Related
I'm using the following batch code to convert all files in a certain directory if the target file doesn't already exist however I'm stuck at getting this to run through every submap and file within that (and keep the output relative with that submap)
So I currently use this:
for %%f in (input/textures/*.*) do ( IF NOT EXIST "ouput/textures/%%~nf.dds" (
"bin/ThempImageParser.exe" "input/textures/%%f" "ouput/textures/%%~nf.dds"
)
)
This works perfectly for a single folder (as was intended), it takes all the files in that specific folder, and passes them as arguments to my executable, which then outputs the file on the path of the second argument.
However this also contains a flaw (this is an additional problem though..) as it does not work if the output -folder- does not exist, so if possible I'd also want it to create the folder if need be.
I've found some batch documentation (I really don't have much experience with Batch) showing me a command called FORFILES and the /R parameter, however I couldn't adjust this so it'd keep the relative paths for the output too, it'd require string manipulation and I have no clue on how to do that.
So the result I'm after is something like this, it takes any file deeper than "input/textures/ for example:
input/textures/some/very/deep/submap/why/does/it/go/on/myfile.anyExtension
it should then take that file (and relative path) and basically change "input" with "output" and replace the file extension with .dds like this:
ouput/textures/some/very/deep/submap/why/does/it/go/on/myfile.dds
and pass those two strings to my executable.
#ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir\wherever\something"
FOR /f "delims=" %%a IN ('xcopy /y /L /s "%sourcedir%\*"') DO (
SET "destfile=%%a"
SET "destfile=!destfile:*%sourcedir%=%destdir%!"
IF /i "%%a" neq "!destfile!" (
FOR %%m IN ("!destfile!") DO IF NOT EXIST "%%~dpm%%~na.dds" (
ECHO MD "%%~dpm"
ECHO "bin\ThempImageParser.exe" "%%a" "%%~dpm%%~na.dds"
)
)
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
First, perform an xcopy with the /L option to list-only the individual fullnames of files that would be copied by the xcopy.
Assign each name found from %%a to destfile, then remove all characters before the source-directoryname from that filename, and replace that string with the destination directoryname.
This will yield the destination name for the file (with the original extension). The only exception will be the very last output line, which is a count-of-files report. Since this line will not contain the source directoryname, the replacement will not take place, so %%a will be the same as !destfile! - so we eliminate that.
Now assign the destination filename to a metavariable so we can select its various parts, and if the filename made from the destination drive and pathname, the name part of the original file and .dds does not exist, then make the destination directoryname and execute the imageparser, providing the desired output filename.
Note that these last two are ECHOed instead of being executed for testing purposes. Remove the ECHOes to actually perform the command.
Note that / is a switch-indicator, \ is a directory-separator.
Note that MD will report an error if the directory already exists. Append 2>nul to the end of the md command to suppress that error message.
I have a .txt file which contains a very long generated list of third level folders for which I want to zip up the contents of each one. So a simplified example, this would be the contents of list.txt
F:\Folder\2005-05-11\[ABC #1] FolderA
F:\Folder\2005-05-11\[ABC #2] FolderB
F:\Folder\2005-05-26\[ABC #1] FolderA
If I do this
for /F "delims=" %%X in (list.txt) do "7z.exe" a -mx=0 "%%X.zip" "%%X\*"
The filename of each zip is just the final directory that is being zipped. So [ABC #1] FolderA.zip and so on. All of these zips are going to be later moved out of this context and so I need to more clearly name them.
In an absolutely ideal world I'd like them to take the 2nd level name (the date) and just the part in square brackets from the last folder (Which is always consistent). So 2015-05-11 [ABC #1].zip but I don't know if that is possible in some way.
The seemingly easier option therefore is just including that parent directory onto the zip, so 2005-05-11 [ABC #1] FolderA.zip, how could I do that?
Obviously I'm using 7zip in my current script but if it's easier with WinRAR or another tool that's fine.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q42060809.txt"
for /F "usebackqdelims=" %%X in ("%filename1%") do (
FOR /f %%d IN ("%%~dpX.") DO (
FOR /f "delims=[]" %%b IN ("%%~nX") DO (
ECHO("7z.exe" a -mx=0 "%%~dpX%%~nxd[%%b].zip" "%%X\*"
)
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q42060809.txt containing your data for my testing.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, remove the ECHO( to actually zip the files.
Naturally, you don't need the usebackq if you don't "quote the filename"
Step 2 - append . to the ~dp date and path portions of %%X. The result in %%~nxd is the Name and eXtension of the lowest level of directory.
Then, with the ~n (Name) portion only of %%X, tokenise it using the brackets as delimiters, so the part between the brackets will appear in %%b
Then mix and match the appropriate elements. Re-insert the brackets if required and add a space before the open bracket if that suits your fancy...
As a part of my job I frequently receive large numbers of image files that contain hyphens or underscores which need to be removed.
I have tried using the below code, and it worked when I ran it within a folder on my dropdox. However, I tried running it on a folder in our network share, and it is not working properly. It looks like it is stripping the characters from a few of the files, and then delivering the message "A duplicate file name exists, or the file cannot be found."
Running the file again strips the characters from a few more files before delivering the same message.
#echo off
pushd %~dp0
setlocal enabledelayedexpansion
for %%j in (*.*) do (
set filename=%%~nj
set filename=!filename:_=!
set filename=!filename:-=!
if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
)
i have list of files in test.txt which contains list of file paths in format d:\source\www\default.aspx;d:\source\common\common.js I need to write a bat file to copy these files to destination eg.F:\destination\ whose path is also passed as an parameter to the bat file.I have following script for this for /f %%l in (somefile.txt) do (
for %%f in (%%l) do (
copy "%%f" %1
)
) issue is i need to keep the copy source folder's folder structure in destination folder too. ie above d:\source\www\default.aspx need to copy to f:\destination\www\default.aspx not to f:\destination. Will gratefull if some one can give solution to this.
Please try with xcopy /I "%%f" "%~1\%%~pf":
xcopy will create the directory structure for you (without prompting because of the /I switch);
%%~pf is the path-only part of the file to copy (see help for), appended to your destination base path without any surrounding quotes %~1;
the destination path combination is enclosed in quotes.
I have many folders in a directory that contain various files. Each filename begins with XXX_ where XXX could be the name of the folder the file is in. What I am needing to do is to go through all those folders and delete any file where XXX is the name of the folder that file is in.
Please have an eye out this question: Iterating through folders and files in batch file?.
I think this should help you.
Please let me know if you need further assistance.
EDIT #1
The joker character in DOS command line is *. Then, while searching a directory for certain files, you may consider your regular expression, that is, your XXX_, and complete it with *, this shall return only the files for which you're looking for.
This means that instead of *.zip pattern in one of the FOR loops given the linked question, your first FOR loop should contain your directory name, then take this variable concatenated with the * character to obtain only the files you're looking for.
For example, consider trying the following:
dir /s XXX_*.*
This should return only the files you're interested in, given the right folder name.
EDIT #2
Thanks for having precised your concern.
Here is a code sample that, I do hope so, should help. Now I know you say you have the looping correct, so that perhaps only piece of this code might be needed.
#echo off
setlocal enableextensions enabledelayedexpansion
for /F "delims==" %%d in ('dir /ogne /ad /b /s .') do (
for /F "delims==" %%f in ('dir /b "%%d\%%~nd_*.*"') do (
echo %%d\%%f
)
)
endlocal
This works and lists the files contained in subfolders from the current (.) folder.
I have tested it from the following folder:
C:\Docume~1\marw1\MyDocu~1\MyMusi~1
Where a 'XXX' folder is contained. This 'XXX' folder contains the following files:
Copy of XXX_blah.bmp;
XXX_blah.bmp;
XXX_1234.ppt;
XXX_textfile.txt.
From this structure, the output is:
C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_blah.bmp
C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_1234.ppt
C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_textfile.txt
I then suspect that putting a del instruction instead of an echo command shall do the trick. This means that to isolate the foldername itself from its path, you need to use the ~n instruction with your folder variable name like %%~nd, where your iterating folder variable name is %%d.
Furthermore, you could even use a parameterized batch file in the process, instead of hardcoding it, that is, if your 'set YourFolder =...' is part of your production code. This could look like:
#echo off
setlocal...
set root = %1
set root = %root:~1%
set root = %root:~0,-1%
...
endlocal
Instead of having '.' as pictured in my first FOR loop, your would replace it with "%root%" in order to consider your command line parameter instead of a hardcoded filepath.
I do help this helps, sincerely!
As Ron says, since the question is tagged "windows".
EDIT:
Ron's answer, which seems to have disappeared!, was to use del /s
EDIT2:
OK, it's valid only for file names, not for directories. For the directories you'd have to use something like sketched below.
Additional info: when you want to do the same thing recursively to files in a directory tree, and (unlike del) there's no command that already does the traversing for you, you can use the /R option of the for command.
To see the for command's docs, do e.g. start "for-help" cmd /k for /?.
Cheers & hth.,
– Alf
cd C:\"foldername"
del /s XXX_"*"
cls
exit