I have a folder with a bunch of subfolders. How would I wirte a bat file to append 8 random characters to the end of the folder names.
The first step I went through was placing files into the subfolders below, with this code:
for %%i in (PathToWorkingFolder\*) do mkdir "PathToWorkingFolder\%%~ni" & move "%%i" "PathToWorkingFolder\%%~ni"
The folder names are :
FD3_2012-10_Stmt
FD3_2012-10_Tax
FD3_2012-10_Warr
The two steps may be achieved in the same FOR:
#echo off
setlocal EnableDelayedExpansion
for %%i in (PathToWorkingFolder\*) do (
rem Get folder name with 8 random digits at end
digits=000000!random!!random!
set "folder=%%~Ni!digits:~-8!"
rem Create the subfolder and move the file
mkdir "PathToWorkingFolder\!folder!"
move "%%i" "PathToWorkingFolder\!folder!"
)
Related
I currently have a script I have been working on for a few hours and having some trouble. What it does is takes all .dat files in the C:\TEST\WF\ directory and renames it to the folder name plus .txt. However it is not renaming the files to the folder name, it grabs the previous folder instead
Example:
Source Directory:
C:\TEST\WF\FOLDER1\FILE.dat
C:\TEST\WF\FOLDER2\FILE.dat
C:\TEST\WF\FOLDER3\FILE.dat
Current output:
C:\TEST\WF\FOLDER1\WF.txt
C:\TEST\WF\FOLDER2\WF.txt
C:\TEST\WF\FOLDER3\WF.txt
Expected output
C:\TEST\WF\FOLDER1\FOLDER1.txt
C:\TEST\WF\FOLDER2\FOLDER2.txt
C:\TEST\WF\FOLDER3\FOLDER3.txt
#echo off
setlocal EnableDelayedExpansion
for /d %%F in (C:\TEST\WF\*) do (
for %%* in (%%F) do set CurrDirName=%%*
IF EXIST "%%F\*.dat" (REN "%%F\*.dat" %CurrDirName%.txt)
)
I have about 10 batch files which copy *.txt files from root folder to their subfolders. The code for copying is following:
cscript ..\myScripts\CopyFiles.vbs "." "..\myScripts\Copy.cfg" "scripts_A" "((.+\.txt)|(.+\.ps1))"
Every single batch file copy *.txt files to folder with the same name (A.bat ->scripts_A, B.bat ->scripts_B). I need to have right filename in each subfolder (in folder scripts_A files need to be operationAfirst.txt, operationAsecond.txt, in folder scripts_B files operationBfirst.txt, operationBsecond.txt). So the result should be following:
for folder scripts_A:
operationAfirst.txt
operationAsecond.txt
for folder scripts_B:
operationBfirst.txt
operationBsecond.txt
All *.txt files in subfolders are usually clones of *.txt files for folder scripts_A. I just need to update their filename. Is there any posibility how to edit batch file, which can replace "A" string in filename for "B" in batch used for folder scripts_B?
Instead of repairing the outcome of a poorly designed setup, I'd prefer to give hints how to alter that situation. So against the best of one's knowledge this batch will do the rename:
#Echo off
Setlocal
CD /D X:\where\ever\you\start
For %%A in (A B C D E F G H I J) Do For /F "delims=" %%B in (
'Dir /B %CD%\Script_%%A\operation*.txt |Findstr /V "operation%%A"'
) Do Call :RenFiles %%A "%%~fB"
Goto :Eof
:RenFiles
Set OldName=%~nx2
Set NewName=operation%1%OldName:~10%
Echo Ren %2 %NewName%
Change the path in the cd statement.
The first of the stacked for loops iterates the letters A to J.
The second one executes dir in the folder script_(letter) listing all
files starting with operation and the extension .txt. The findstr
then removes all correct names for this folder.
The remaining are then arguments to the called subroutine
The subroutine builds the new name from the prefix operation
followed by the folder letter and the original name from 11th place
(offset 10).
As long as the echo remaines in front of the ren it only shows what it would do.
I have a folder where lots of .tif files drop into.
They all start with MW_ or SW_ and later also NSSW. So I need to be able to expand from the first two later to NSSW.
I have a batch file already that first moves the files into the folder MW or SW based on the first 2 characters of the file name. Here is my current batch file and it works just fine. But I think I need a second batch file or addition to this to do the below 1 & 2 steps. Please see below, after this code.
REM Sort by First name.
REM This script creates a folder for either the full file name,
REM or if it contains an underscore, the part before the underscore.
REM TODO - Don't copy over existing files.
REM TODO - Move files into Sub folders based on Date in file name "last 8 characters .tif
#echo off
REM Needed because you are working with variables that are immediately called
setlocal enabledelayedexpansion
REM Start of the loop to get all files with a psd or jpg Extension
for %%A in (*.tif *.jpg *.pdf) do (
echo file found %%A
REM Grabs only the file name
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
REM Grabs only the extension
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
REM Checks for the existence of the folder, if the folder does not exist it creates the folder
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
REM Moves the file to the folder
echo Moving file %%A to folder !folname!
REM if not exist "%%D\%%A" move "%%A" "!folname!"
if not exist "%%D\%%A" copy "%%A" "!folname!"
REM add the date DDMMYYYY to the end of each file. Name can be 80 characters long.
rem ren "!folname!\%%A" "????????????????????????????????????????????????????????????????????????????????_%date:~-10,2%%date:~-7,2%%date:~-4,4%.tif"
)
echo Finished
pause
So here is what I need I guess. A second or 3rd batch file to do the below. I hope, someone can help.
Note: Please keep in mind that if a file exists it renames the copy when moving with xxxx(1).tif, xxx(2).tif, etc. on the end of file name.
Move files now listed in MW folder into new or existing subfolders based on the name of the file from the 4th to 10th character of the file name or from the 4th character to the second or next "_".
Move file in subfolder of the named folder to a sub date named folder based on last 8 characters of file name.
What I need is to then move the files from the MW folder into new or existing subfolders based on the last 7 characters of the file name "the date section".
For example we start with MW files
Files coming in to folder C:\temp\Test_Moving_Files\
MW_VRL5VF10000_6542234_01052016.TIF
MW_Flybuys_677888_01052016.TIF
MW_VRL5VF10000_333443_02052016.TIF
MW_Flybuys_555555_02052016.TIF
MW_goodguys_534535_02052016.TIF
MW_goodguys_222222_02052016.TIF
MW_Flybuys_123443_03052016.TIF
MW_Flybuys_3545555_03052016.TIF
MW_goodguys_444444_03052016.TIF
MW_goodguys_888888_03052016.TIF
Output to subfolders should be sorted to subfolders like below:
MW\VRL5VF10000\01052016\MW_VRL5VF10000_6542234_01052016.TIF
MW\VRL5VF10000\02052016\MW_VRL5VF10000_333443_02052016.TIF
MW\Flybuys\01052016\MW_Flybuys_677888_01052016.TIF
MW\Flybuys\02052016\MW_Flybuys_555555_02052016.TIF
MW\Flybuys\03052016\MW_Flybuys_123443_03052016.TIF
MW\Flybuys\03052016\MW_Flybuys_3545555_03052016.TIF
MW\goodguys\01052016\MW_goodguys_222222_02052016.TIF
MW\goodguys\02052016\MW_goodguys_534535_02052016.TIF
MW\goodguys\03052016\MW_goodguys_444444_03052016.TIF
MW\goodguys\03052016\MW_goodguys_888888_03052016.TIF
It looks the task can be achieved by modifying
REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
REM Checks for the existence of the folder, if the folder does not exist it creates the folder
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
to
REM Using the file name it separates it into 4 parts using "_" as a delimiter.
for /f "tokens=1-4 delims=_" %%D in ("!fname!") do set "folname=%%D\%%E\%%G"
echo Folder name !folname!
REM Checks for the existence of the folder path. If the folder
REM path does not exist, it creates the complete folder structure.
if not exist "!folname!\*" (
echo Folder !folname! does not exist, creating ...
md "!folname!"
) else (
echo Folder !folname! exists.
)
Instead of using just first substring of underscore delimited file name, the code above uses now first, second and fourth substring. I did not test it!
Command MD can create also multiple folders at once and therefore variable folname can be also a folder path string. Well, I suggest to rename the variable folname in your batch code for example to FolderPath.
Of course you must use folname or better FolderPath also in rest of batch script instead of %%D.
I have a bunch of folders (and subfolders) in the structure of...
Test/Student001/ABC,
Test/Student001/DEF,
Test/Student002/ABC,
Test/Student002/DEF, etc...
What I then need to do is relocate those folders (and their subfolders and the files within) to another location so that it is like...
Test/Class01/ArronAmos(Student001)/ABC
Test/Class01/ArronAmos(Student001)/DEF
Test/Class02/BrettBesty(Student002)/ABC
Test/Class02/BrettBesty(Student002)/DEF
I have text files with all the folders (and subfolders) original and new names (saved like so)..
studentdata.txt
A (studentcode), B (studentnewname), C (Class)
Student001, ArronAmos (Student001), Class01
Student002, BrettBesty (Student002), Class02
Is there a way to get a batch to basically go like this (using A,B and C from text file above - preferably the one txt file if possible)...
md 'C' ::which will skip past if folder exists
rename folder 'A' to 'B' ::only rename of root folder and leave subfolders intact
move folder 'B' to 'C' ::move new named folder (B) and all subfolders and contents to new root folder (C)
The creation of the new directories and subfolders (for new and future students) goes like this and works great (except if there was a way to call a 2nd text file for the subfolder creation rather than coded in that would be awesome - but no biggy I guess)...
The Creation Batch
cd /d %~dp0 pause
FOR /F "delims=~" %%f in (dirlist.txt) DO md "%%f"
:: This code creates directories referenced from a .txt file: - :: FOR /F "delims=~" %%f in (dirlist.txt) DO MD "%%f"
pause
FOR /D %%x in (*) do mkdir "%%x\Individual Behaviour Plans" "%%x\Individual Learning Plans" "%%x\Student Reports" "%%x\Student Support Group Meetings"
:: This code creates a new dir within every folder in batch location: - :: FOR /D %%x in (*) do mkdir "%%x\value"
pause
and this is the Rename Batch that I received off one of the other techs and don't quite understand it or know to modify it to make it work..
*rename_users.bat** :: Script to Rename folders - prefixing from a text file
setlocal ENABLEDELAYEDEXPANSION
Set Rootfolder=Test Set Names=names.txt
:: Goto Root Folder cd /d %~dp0
:: Start Line Counter Set LineCount=0
:: For Every folder in the directory
For /d /r %%g in (*) DO (
:: Increment the line counter by 1 (see the use of "!" >instead of "%" due to delayed expansion)
Set /a LineCount=!Linecount!+1
:: Call the Rename Folder sub - passing through the >variables of folder name and line counter
Call:RenameFolder %%g !LineCount!)
:RenameFolder :: For all of the tokens in the findstr on the names file for /f "Tokens=1* delims=:" %%a in ('findstr /n "^" "%Names%"') DO ( :: If the line counter matches the line number If %%a==%~2 ( :: Rename the Folder Move "%~1" "%~1 %%b") ) ::Return to the Primary
Goto:EOF
Set Rootfolder= Set Names= Set linecount= Set Drive=
Endlocal
The trick is we can't just use the create directories (and subdirectories) batch file, as there are some folders existing in the original format that have data in them that we need sitting in the new structures subfolders... and moving them manually would be an option... if there were not 900+ student folders to do this too...
I hope makes some form of sense... Thanks guys!
The Batch file below do what you want:
#echo off
setlocal EnableDelayedExpansion
rem Enter into working directory
cd C:/Test
rem Process the file with original and new names
for /F "skip=1 tokens=1-3 delims=," %%a in (studentData.txt) do (
rem Get studentNewName end eliminate spaces
set "studentNewName=%%b"
set "studentNewName=!studentNewName: =!"
rem If the new folder not exist, create it
if not exist "%%c/!studentNewName!" md "%%c/!studentNewName!"
rem Move files from old folder
move "%%a/*.*" "%%c/!studentNewName!"
rem And delete old empty folder
rd "%%a"
)
Antonio
I think it's a simple task, but I'm a noob to batch scripting.
Here is what the structure looks like:
MAINDIR
directory name
nameof.file
I'm looking to batch rename all the files in the directories the same name as the directory.
Basically it is:
Scan the subfolders and find all *.file.
Rename the *.file to the name of the directory it is already in
The end result is nameof.file to be directory name.file
To be more specific, I have 350 files that i need to rename. They are all in their appropriate directory and I want the 1 file inside each folder to have the name of the folder as the file name.
c:\folder 1\file1.ext
c:\folder 2\file2.ext
c:\folder 3\file3.ext
All the .ext files need to have their folder names as file name.
c:\folder 1\folder 1.ext
c:\folder 2\folder 2.ext
c:\folder 3\folder 3.ext
I hope that makes it more clear. So once the script is prepared, I can drag and drop all 350 files and one by one onto the script and it will take the file and rename it.
This will rename any file that is dragged onto it to it's parent directory name.
setlocal enabledelayedexpansion
for %%a in (%1) do (
set p=%%~dpa
set p=!p:~0,-1!
for %%b in ("!p!") do ren %%a "%%~nb.*"
)
To do this recursively through all files in all subfolders under a fixed root directory you can use this
setlocal enabledelayedexpansion
for /r C:\folder %%a in (*) do (
set p=%%~dpa
set p=!p:~0,-1!
for %%b in ("!p!") do ren "%%a" "%%~nb.*"
)
Which will save you having to drag and drop them all individually. Unless you can only do it one at a time, in which case the first solution will suffice.