I need a way to make a directories containing the filename, but counting +1 for every one created.
How can i add +1 in front of the directory name created
Eg:
if Directory 1_xxx is present make 2_xxxxx
Currently i am using below to create a directory from Filename
File name Eg: Card_24ALL Sms.csv
Directory Output Eg: Card_24ALL Sms
Currently i have directories adding +1 manually 1_
1_Card_24ALL Sms ( With File Card_24ALL Sms.csv copied inside)
2_Card_25ALL Sms ( With File Card_25ALL Sms.csv copied inside)
3_Tjek_24ALL Sms ( With File Tjek_24ALL Sms.csv copied inside)
Using the below Code i can get directory added containing the given filename:
#echo off
for %%a in (*.csv) do (
md "%%~na" 2>nul
move "%%a" "%%~na"
)
But I have to manually create the number in front to get number_directory as the number in front can be 99_
Try this:
#echo off
setlocal enabledelayedexpansion
#echo off
cd /d c:\temp
if exist cnt.txt for /f %%a in (cnt.txt) do set %%a
for %%a in (*.csv) do (
set /a cnt+=1
echo md "!cnt!_%%~na" 2>nul
echo move "%%a" "!cnt!_%%~na" >nul && (
Echo File %%~nxa moved to !cnt!_%%~na) || (Echo Move failed.)
)
>cnt.txt echo cnt=!cnt!
Remove the echo's from md and move once you verify the output.
Related
I'm trying to create a batch file script that will compress all subfolders named folder1 within a directory, BUT it should only compress this folder if it does NOT contain another subfolder with the same name.
I am very new to cmd and batch files, and this is also my first post of stack overflow, please let me know if I've failed to give some information that I should!
The bit of pseudo-ish code below hopefully illustrates what I'm trying to accomplish:
#echo off
SETLOCAL EnableDelayedExpansion
FOR /D /R %%G IN (*folder1*) DO (
CD %%G
SET /A compress=true
FOR /D /R %%H IN (*folder1*) DO (
ECHO folder contains another folder of same name, should not be compressed
SET /A compress=false
)
IF !compress!==true (
ECHO Run compression operation on folder
"C:\Program Files (x86)\7-zip\7z.exe" a -tzip "%%G.zip" "%%G\"
)
)
Please ask away if anything seems unclear! I'm really hoping to turn the above into functional code, thank you in advance for any input or thoughts.
#ECHO OFF
SETLOCAL
:: Starting directory
SET "sourcedir=U:\sourcedir"
:: name of directory to compress
SET "targetname=targetdir"
FOR /d /r "%sourcedir%" %%a IN (*) DO (
IF /i "%%~nxa" == "%targetname%" IF NOT EXIST "%%a\%targetname%\." (
ECHO compress %%a
rem temporarily switch to target directory
PUSHD "%%a"
ECHO 7z a -tzip "%%a.zip"
rem back to original directory
POPD
)
)
GOTO :EOF
This should do as you want - it will echo not execute the 7z command.
The if sees whether the "name and extension" portion of the directory-name in %%a matches the target (the /i makes the match case-insensitive). If it matches AND there is a subdirectory with the required name, then the compression portion is executed.
There are two points to consider.
First, the name of the destination ZIP file. As you have written it, the ZIP generated would be folder1.zip in the parent directory. IDK what you want here. This code would do the same, but %%a.zip could be replaced by ..\%targetname%.zip since the pushd/popd changes the current directory to the folder1 directory and .. means the parent directory.
The second matter is whether or not you want to compress ...\folder1\folder1 (which would have a destination ZIP file of ...\folder1\folder1.zip)
Revision given comment:
#ECHO OFF
SETLOCAL
:: Starting directory
SET "sourcedir=U:\sourcedir"
:: name of directory to compress
SET "targetname=targetdir"
REM (
FOR /d /r "%sourcedir%" %%a IN (*) DO IF /i "%%~nxa" NEQ "%targetname%" (
rem calculate parent name in %%~nxp and grandparent in %%~nxg
FOR %%p IN ("%%~dpa.") DO FOR %%g IN ("%%~dpp.") DO (
IF /i "%%~nxp" == "%targetname%" IF /i "%%~nxg" NEQ "%targetname%" (
ECHO child %%~nxa
ECHO parent %%~nxp
ECHO Gparent %%~nxg Gppath=%%~dpg
ECHO compress %%a
rem temporarily switch to target directory
PUSHD "%%a"
ECHO 7z a -tzip "%%a.zip"
ECHO -------------------
rem back to original directory
POPD
)
)
)
GOTO :EOF
It's not that clear what you want to do with a directory named ...\folder1\folder1\something or what the target ZIP file-name should be.
The if in the for /d /r line will ensure that only leaf-names that do not match the target name are processed. The path-name in %%a is then processed into the parent and grandparent portions - note that %%~dp? is the drive+path portion of %%? which terminates \ so appending . to this resolves to effectively removing the terminal \ yielding a "filename".
You appear to want to compress directories that have a parent but not a grandparent named with the target string, hence the innermost if statement.
I've just echoed the various strings available at this point so they may be strung together as required to form your destination ZIP file-name. Note that the pushd/popd bracket ensures that the current directory at the time of the compress is the leaf to be compressed.
I am in the middle of batch extracting screenshots for contents we are planning to use on a tube site I am working on.
The jpeg files per content is labled as followed:
6c82c0239f6eb839-1
6c82c0239f6eb839-2
all the way to 120
The file name is different per content
a82384e2c46ba4af-1
a82384e2c46ba4af-2
etc.
They will all be extracted to a singe folder.
So I basically need a batch file that will create folders based on the content name without the dash and number and move all 120 jpegs in the folder with the content name.
For example:
Create folder named 6c82c0239f6eb839 and
move 6c82c0239f6eb839-1 to 6c82c0239f6eb839-120 in to the created folder.
I saw another thread with the following batch file. its pretty much what I want but the folder name is only 3 characters long and the files are copied to the newly created folders instead of moving them.
#echo off
SetLocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
set Folder=!Name:~0,3!
xcopy /y "%%a" !Folder!\
)
Could someone change this so that it will display full file name without the dash and number for the folders and move files in its respective folders instead of copy?
Thank you
#echo off
setlocal
#rem Get each jpeg file.
for /F "delims=" %%A in ('2^>nul dir /b *.jpeg') do (
rem Get filename as token before the dash.
for /f "delims=-" %%B in ("%%~A") do (
rem Make dir if needed.
if not exist "%%~B" md "%%~B"
rem Check if isdir.
2>nul pushd "%%~B" && popd
if errorlevel 1 (
>&2 echo Failed isdir "%%~B".
) else (
rem Do the move operation.
>nul move /y "%%~A" "%%~B"
if errorlevel 1 (
>&2 echo Failed move "%%~A" to "%%~B"
)
)
)
)
exit /b %errorlevel%
The code is well remarked so if you want to understand
the evaluated code by changing #echo off to #echo on.
The use of %errorlevel% after the exit /b is not
required though will let you know what the errorlevel is
when #echo on is used.
The pushd tests for a directory
(even if it is a symlink).
errorlevel is checked to decide if to echo a
error message or do the move.
As the for loop variables are used direct, use of
enabledelayedexpansion is not needed.
Many commands support the argument of /? to get help
about the command. i.e. move /?.
If you only try to copy the correct jpeg to the correct folder, you can do this:
#echo off
SetLocal EnableDelayedExpansion
CD <CORRECT ROOT PATH>
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
REM I presume all the files have 16 characters before the dash
set Folder=!Name:~0,16!
IF NOT EXIST !Folder! MKDIR !FOLDER!
xcopy /y "%%a" !Folder!\
)
I was not able to test.
First of all, I would like to apologize for my manners regarding my initial post.
The answer by micheal_heath has resolved my issue.
Furthermore, I happened to find this post by user Salmon Trout from a different site which also worked.
Batch file to make folders with part of file name and then copy files
#echo off
setlocal enabledelayedexpansion
for %%A in (*.psd *.jpg) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
pause
I just changed the the following line remove the hypen and numbers to create folders for the file name properly.
for /f "tokens=1* delims=-***" %%D in ("!fname!") do set folname=%%D
I still lack the knowledge on why and how both methods work, but this has been an interesting start for me. I hope other beginners trying to solve a similar issue can find something useful from this post.
I want to create such scenarios in which there are two folders & subfolders of A and B, when there is an addition of files or data in folder A it should create the same files or data in folder B (including subfolder). Also, for example, there are 6 files in folder A and 7 files in folder B system automatically check the files and subfiles of folder A and B and delete the mismatched file(pqr.js
) from 'Folder B'. We also want to create a bridge or validator text file between folder A and B which validate the file names, for example if there is filename 'config.properties' which is present in text file (Validator) ABC.txt and that file is not present in folder A system should check the file name in validator before deleting any files from folder B. So if there are no such file name config.properties in folder A but present in validator text file system will keep that file in folder B though it is deleted from folder A.
set dirsrc=folder A
set dirdest=folder B
set validator=ABC.txt
setlocal ENABLEDELAYEDEXPANSION
echo Source: "%dirsrc%"
echo Destination: "%dirdest%"
echo.
echo.
echo Cleaning files...
for /f "tokens=*" %%a in ('dir /s /b /a:-d "%dirdest%"') do (
set curdest=%%a
set cursrc=!curdest:%dirdest%=%dirsrc%!
if not exist !cursrc! (
echo Deleting "!curdest!"
del "!curdest!"
)
)
echo.
echo.
echo Cleaning directories...
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
rem Two lines left empty for NewLine to work
for /f "tokens=*" %%a in ('dir /s /b /a:d "%dirdest%"') do (
set curdest=%%a
set cursrc=!curdest:%dirdest%=%dirsrc%!
if not exist !cursrc! (
set rmlist=!curdest!?!rmlist!
)
)
set rmlist=!rmlist:?=%NL%!
for /f "tokens=*" %%a in ("!rmlist!") do (
if exist %%a (
echo Removing directory "%%a"
rmdir "%%a"
)
)
xcopy /i /y /e "%dirsrc%" "%dirdest%" /d
endlocal
All the scenarios are completed expect Validator scenarios.
Under the path U:\test\0014* I have 99 folders and each of them has respectively 2 subfolders MASTER and DERIVATIVE_COPY. With a following script I try to count the number of PDFs in MASTER folder. If there are only one .pdf file there I want to copy it to DERIVATIVE_COPY folder. IF there are 0 or >1 .pdf in MASTER I want only to show the number of them. This operation should be done for each of the 99 folders.
#echo off
setlocal enabledelayedexpansion
for /R U:\test\0014\*\MASTER %%i in (*.pdf) do (
set /a anzahl+=1
)
if !anzahl! EQU 1 ( echo !anzahl! )
if NOT !anzahl! EQU 1 ( echo !anzahl! )
pause
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=U:\sourcedir"
for /f %%i in ('dir /ad /s/b "%sourcedir%\master*"') do IF /i "%%~nxi"=="MASTER" (
SET /a found=0
FOR %%x IN ("%%i\*.pdf") DO SET /a found+=1
IF !found!==1 (
XCOPY /y "%%i\*.pdf" "%%i\..\derivative_copy\" >nul
) ELSE (
ECHO !found! .pdf files found IN "%%i"
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
First, execute a dir to get a directory-list of the "files" names that start master in the specified tree. Use /ad to select only the directory-names. Accept only those names where the "name+extension" is master (disregarding case)
For each directory-name found, set found to 0 then increment found for each .pdf file found in the directory %%i.
If the resultant count in found is 1, xcopy the file found to the destination subdirectory (which conveniently creates the subdirectory if it doesn't already exist), using /y to overwrite any existing file of the same name and >nul to make the process silent.
Otherwise, report the directoryname and count of files.
I need help creating a batch script that will add the filename to the first line of every .csv within all subfolders. The batch file will be in the mainfolder. The same folder will contain several subfolders, each containing a large amount of .csv files. I need the files to stay within their respective folders. My goal is to add this to another working script I have that merges all files within a subfolder into a single .csv. This will provide a way to navigate through the merged file. I found some similar posts, but they weren't quite what I was looking for. This is the closest thing I could find but the delimiter stuff is throwing me off Batch file to Write Filename to first line
File Structure Example:
C:\MainFolder\batch_script.bat
C:\MainFolder\SubFolder1\csv1.csv
C:\MainFolder\SubFolder1\csv2.csv
C:\MainFolder\SubFolder2\csv3.csv
C:\MainFolder\SubFolder2\csv4.csv
File Contents before running script
csv1.csv:
This is the content of csv1.csv
csv2.csv:
This is the content of csv2.csv
csv3.csv:
This is the content of csv3.csv
csv4.csv:
This is the content of csv4.csv
File Contents after running script:
csv1.csv:
csv1.csv
This is the content of csv1.csv
csv2.csv:
csv2.csv
This is the content of csv2.csv
csv3.csv:
csv3.csv
This is the content of csv3.csv
csv4.csv:
csv4.csv
This is the content of csv4.csv
P.S.
I also found this code on another website but I'm not sure how I would make it loop through the different subfolders
FOR /F "usebackq tokens=1" %%i IN (`DIR /B`) DO (
ECHO %%i >> %1
TYPE %%i >> %1
)
#echo off
setlocal enableextensions disabledelayedexpansion
for /r %%a in (*.csv) do (
set /p "firstLine=" < "%%~fa"
setlocal enabledelayedexpansion
for /f "delims=" %%b in (".!firstLine!") do endlocal & if not "%%~b"==".%%~nxa" (
( echo %%~nxa
type "%%~fa"
) > "%%~fa.new"
move /y "%%~fa.new" "%%~fa" >nul
)
)
What it does is generate a new file with the name of the csv as the first line, appends the csv to the new file and moves the new file over the csv. In the process, it tests if the content of the first line is the same as the name of the csv to avoid reinclusion of the filename on each run of the batch file.
edited to adapt to comments - As the process will not be run more than once for set of csv files, the full code can be reduced to
#echo off
setlocal enableextensions disabledelayedexpansion
for /r %%a in (*.csv) do (
( echo %%~nxa
type "%%~fa"
) > "%%~fa.new"
move /y "%%~fa.new" "%%~fa" >nul
)
Try this:
for /r %%a in (*.csv) do (
copy /y "%%~a" "%temp%\%%~na"
Echo %%~na > "%%~a"
for /f "usebackq" %%b in ("%temp%\%%~na") do Echo %%b >> "%%~a"
del "%temp%\%%~na"
)
And that should do what you want it to. I would test it out first if you could.
This should create an all.csv in every folder in the tree under the C:\MainFolder and add the filename before the contents of each CSV file.
The filename will contain the full path but that can easily be changed if needed.
#echo off
for /d /r "C:\MainFolder\" %%a in (*) do (
pushd "%%a"
(for %%b in (*.csv) do (
echo %%b
type "%%b"
)
)>all.tmp
ren all.tmp all.csv
popd
)
pause