How do I use an existing folder hierarchy structure as template to create new folder with the same hierarchy structure? - right-click

I have created a working script that I've added to the windows registry. When a person right clicks in windows explorer, they can click on "Create New Job Folder" that creates a new folder with a specified hierarchy but it asks the user for the name of the folder.
I'm trying to figure out how to edit my current script to create the hierarchy based on a certain folder. This folder will basically be a template so as I update the folder structure to better suit my needs, each subsequent folder I create using the script will reflect those changes. Thanks in advance for any help!
Here is what I have in my current working script:
set verbose=NO
#echo off
set mydir = %cd%
cd %mydir%
cls
call:main
goto:eof
:main
set /p job=Enter Job Name:
call:check
if %choice%==2 (echo Creating folder. Please Wait... & timeout 1 > nul &
call:createDirectory)
goto main
:check
echo[
if exist "%job%" (echo ERROR: Folder already exists. Please choose a different name.
& set choice=1) else (set choice=2)
echo[
exit /b
:createDirectory
md "%job%"
md "%job%"\1.Development
md "%job%"\1.Development\1.Budgets
md "%job%"\1.Development\2.Manufacturers
md "%job%"\"2.Plans and Specs"
md "%job%"\"2.Plans and Specs"\1.Plans
md "%job%"\"2.Plans and Specs"\2.Specs
md "%job%"\3.Pricing
md "%job%"\3.Pricing\1.Quotes
md "%job%"\3.Pricing\2.Worksheets
md "%job%"\4.Sumbittals
md "%job%"\4.Sumbittals\"1.Submittal Reviews"
md "%job%"\5.Orders
md "%job%"\5.Orders\"1.Order Summaries"
md "%job%"\5.Orders\"2.Confirmation Pages"
md "%job%"\6.Closeout
md "%job%"\6.Closeout\"1.O&M Manuals"
md "%job%"\6.Closeout\2.Warranty
exit

You can use FOR /D to list the subfolders of the current directory or FOR /D /R (undocumented? /r flag) to list all subfolders in the current directory recursively.
#echo off
setlocal
set base=c:\mytemplatedir
set mydir=%cd%
goto main
:enumdir
pushd "%base%\%~1\%~2"
for /D %%a in (*) do (
md "%mydir%\%~1\%~2\%%~a"
call :enumdir "%~1\%~2" "%%~a"
)
popd
#goto :EOF
:main
call :enumdir . .
or you can use FOR /R to walk a folder tree for files (*) or folders (.).
If you know your template folders might contain &, ! or % in their names you might want to consider using a .vbs file instead so you don't have to deal with special batch characters.

Related

name files after template bat file - used CMD code

I have a *.bat file in various folders which renames certain files so project number, project name and initials are included in the filename. So far running the file will ask the user to enter project number, project name and initials which are saved as variables (no, project and initial). Each original filename is kept but in brackets will be the required projects specific data). So far it works but what I would like is set up a batch file where the name sets the template for the project specific data e.g.filename of bat file could be:
Rename <XY00.000> (Projectname),XYZ.bat
If in CMD code you could retrieve the filename of the executable bat file (should be only 1 in the folder) and then with delimiters you would splice the filename and store the relevant strings in different variables e.g. what is inside <> will be variable "no", whats inside () will be variable "project" and what is past the , is "initial". If I have those variables the file renaming is as in my current procedure. Please help as I'm not familiar with CMD code or Powershell (which might be alternative option) and it took me long to assemble the existing code. Basically I want to change the input part (see set /p) to something which does:
get filename of bat file
extract 3 text strings from the filename (use of delimiters like ( < ,)
store the 3 text strings as parameters (NO, PROJECT , INITIAL)
the rest of the existing code should then work to rename the files in the folder
Thanks.
Existing code as example below:
cls
echo off
pushd "%~p0" 2> nul
pushd "\\%~p0" 2> nul
echo
attrib -r *.* /s
set /p no= Enter Project Reference in style XY00.000:
set /p project= Enter Project name (without any underscores):
set /p initial= Enter your Initials:
for /f "tokens=1* delims=(" %%i in ('dir /b *.xlsm') do ren "%%i(%%j" "%%i(%no% %project%_%initial%).xlsm"
for /f "tokens=1,2* delims=()" %%i in ('dir /b *.docm') do ren "%%i(%%j)%%k" "%%i(%no% %project%_%initial%)%%k"
I replaced the user input with:
set infovariable=%~n0
which safes the filename of the bat file as a variable and uses it later to rename appropriate files with it. The bat file is named exactly what I want as a text. Job done!

Batch Windows - How to create a loop to rename many files in different folders?

I have PDF files in one location (all in the same folder), which I need to take 3 useful information from the file name.
And I have .jpg files in another location (1 picture per folder) which I need to rename with these information taken from the PDF.
My script is able to find the information, store and rename but it only works for the first file in a directory and then stops.
I need to make it run in a loop until there is either no more PDF files to take information from OR no more .jpg files to be renamed.
Can someone help me to make this script run in a loop?
echo off
setLocal EnableDelayedExpansion
rem User input
SET /P datework= Please type the date you want to work (format yyyymmdd):
rem Folder where the PDFs are located - extract the useful information from file name
cd /D C:\Users\A\Desktop\A_tests\QC\PDF\%datework%\
for %%i in (*.pdf) do (
set RcvLn=%%i
set RcvLn=!RcvLn:~0,4!
set GunStn=%%i
set GunStn=!GunStn:~5,4!
set Node=%%i
set Node=!Node:~10,4!
)
rem Rename the pictures using the values stored on the variables
xcopy /Y "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\%datework%\Node %Node%\*.jpg" "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\%datework%\Node%Node%_RL%RcvLn%_GS%GunStn%.jpg"
You set each variable inside of the loop for each file, but then you do the xcopy outside of the loop which will only do the xcopy once. So we rather do the xcopy inside the loop.
echo off
setLocal EnableDelayedExpansion
rem User input
SET /P datework= Please type the date you want to work (format yyyymmdd):
rem Folder where the PDFs are located - extract the useful information from file name
cd /D C:\Users\A\Desktop\A_tests\QC\PDF\%datework%
for %%i in (*.pdf) do (
set RcvLn=%%i
set RcvLn=!RcvLn:~0,4!
set GunStn=%%i
set GunStn=!GunStn:~5,4!
set Node=%%i
set Node=!Node:~10,4!
echo xcopy /Y "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\!datework!\Node !Node!\*.jpg" "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\!datework!\Node!Node!_RL!RcvLn!_GS!GunStn!.jpg"
)

Batch - move files into alphabetical forders including unicode or 'strange' characters

I have some files and I want to move them inside alphabetical folder NOT previously created. With batch I want generate folders. Theses files must be moved inside these folders following files's first letter
I have a multi language file list inside my directory like this:
中文
alfa
35h
Ĕuid
لعربية
សេវិនខ្មែរ
I try this command to move files into alphabetic folders using first folder letter for ordering
#echo off
setlocal enabledelayedexpansion
for /d %%i in (*) do (
set first=%%i
set first=!first:~0,1!
md !first! 2>nul
if not "!first!" == "%%i" move "%%i" "!first!\%%i"
)
Nothing happens.
This part
for /d %%i in (*) do (
create folders and move folders and not files but I want move files inside generated folders (I don't want create folders previously)
You're currently using for /d which works on directories, not files. Since you have files, you need your for loop to work on them. If you just remove the /d, it will do so. However, note that this means it will also move the batch file itself. If you don't want that, then you'll need to put in logic to exclude it. Something like this:
#echo off
setlocal enabledelayedexpansion
for %%i in (*) do (
set first=%%i
if not "!first!" == "%0" (
set first=!first:~0,1!
md !first! 2>nul
if not "!first!" == "%%i" move "%%i" "!first!\%%i"
)
)
EDIT FOR ! SUPPORT
If the filename contains an exclamation mark, it won't work, because when delayed expansion is enabled, it sees that as part of a variable delimiter. The way to get around it is to assign the filename to a variable before you enable delayed expansion:
#echo off
setlocal
for %%i in (*) do (
set name=%%i
setlocal enabledelayedexpansion
if not "!name!" == "%0" (
set first=!name:~0,1!
md !first! 2>nul
if not "!first!" == "!name!" move "!name!" "!first!\!name!"
)
)

Renaming files using the parent folder/directory name as a variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create a batch script that will rename all the documents in all sub-folder with the parent folder name and then the original file name. For example:
There are 200 folders with unique 4 digit names, all the folders contain a folder called "Data Sheets". I would like to rename all the documents in the Data Sheets folder to include the 4 digit unique id name. So essentially I want the documents to be renamed from data sheet.doc to 1234 data sheet.doc
There are many batch scripts out there that do something very close to what I am trying to do but I have been unable to modify them to do exactly what I need. Any help would be appreciated. This is what I have been trying of similar versions trying to get it to work.
#echo off
for /r %%a in ("S:\Fiscal Yr. 2015\*\*.doc") do (
for /d %%d in (%CD%) do (
set newname=%%~nd%~x1
ren "%%~a" "!newname!%%~Xa"
echo media file in %%~fa renamed to "!newname!%%~Xa"
)
)
Zach
To clarify - this will process every .doc file in any folder called "Data Sheets" and rename the files to add the parent folder-name to "Data Sheets" to the start of the .doc files.
This will echo the rename command for you to confirm that it is correct, before removing the first echo statement.
#echo off
for /r "S:\Fiscal Yr. 2015" %%a in (*.doc) do (
for /f "delims=" %%b in ("%%~pa\.") do (
if /i "%%~nxb"=="Data Sheets" for /f "delims=" %%c in ("%%~pb\.") do (
echo ren "%%a" "%%~nxc %%~nxa"
echo media file "%%a" renamed to "%%~nxc %%~nxa"
)
)
)
pause
Here's a simple example of how you can rename all files in an entire folder structure, including all sub-folders:
#echo off
setlocal ENABLEDELAYEDEXPANSION
cd /d "S:\Fiscal Yr. 2015"
set _cd=%CD%
for /f "delims=" %%i in ('dir *.doc /a-d /s /b') do (
set old_fn=%%~fni
set new_fn=!old_fn:%_cd%\=!
set new_fn=!new_fn:Data Sheets\=!
set new_fn=!new_fn:\= !
echo ren "!old_fn!" "!new_fn!"
)
endlocal
The for command enumerates all the .doc files in in the current folder and sub-folders (\s switch). The \b switch makes it output the filenames using bare format (which in this case includes the full path). The /a-d makes it ignore directories, so only files get renamed.
Using setlocal / endlocal makes it possible to use delayed expansion of variables, and thus we use ! instead of % with the set commands. Delayed expansion ensures that the variables are reevaluated when they are used, instead of using the original value assigned to them.
The echo ren ... makes it echo the rename commands to the console instead of actually performing the renames. You'll want to verify it outputs the right rename statements before you run it for real, and it would be a good idea to backup the folder structure before you do it for real since there is no undo for this type of operation.
The batch file removes the leading path and only keeps the sub-folder's name, adding it before the filename, also removing the Data Sheets folder name from the new filename.

DOS Batch file - Copy file based on filename elements

I need to sort alot of files based on their filename. I would like to use a batch file to do it. I do know what I want but I am not sure of the correct syntax.
Example of filenames I work with: (They are all in the same directory originally)
2012_W34_Sales_Store001.pdf
2012_W34_Sales_Store002.pdf
2012_W34_Sales_Store003.pdf
2012_November_Sales_Store001.pdf
2012_November_Sales_Store002.pdf
2012_November_Sales_Store003.pdf
I would like to extract the information that are located between the "_" signs and put them into a different variable each time. The lenght of the informations contained between the _ signs will be different everytime.
Example:
var1="2012"
var2="W34" (or November)
var3="Sales"
var4="001"
If I am able to do this, I could then copy the files to the appropriate directory using
move %var1%_%var2%_%var3%_%var4%.pdf z:\%var3%\%var4%\%var1%\%var2%
It would need to loop because I have Store001 to Store050.
Also, there are not only Sales report, many others are available.
I hope I am clear.
Please help me realize this batchfile!
This script will make sure that it only attempts to move files that meet the pattern part1_part2_part3_part4.pdf
#echo off
for /f "eol=_ delims=" %%F in (
'dir /b *^|findstr /ix "[^_]*_[^_]*_[^_]*_[^_]*[.]pdf'
) do for /f "eol=_ tokens=1-4 delims=_." %%A in ("%%F") do (
move "%%F" "z:\%%C\%%D\%%A\%%B"
)
If needed, you could add md "z:\%%C\%%D\%%A\%%B" 2>nul before the MOVE in case the folders might not exist yet.
This script will move your files based on the values between the underscore to a like wise constructed path.
for %%f in (*.pdf) do call :handlefile %%f
:handlefile
set pad=z:
for /f "delims= tokens=1,* " %%a in ("%1") do call :step %%a %%b
rem this MOVES the file, maybe use echo first for testing
move "%fn%" "%pad%"
exit /B
:step
if !%2!==!! EXIT /B
set pad=%pad%\%1
for /f "delims=_ tokens=1,* " %%a in ("%2") do call :step %%a %%b
EXIT /B