Batch copy files from different folders based on Date LOOP - windows

I hope someone can help me.
Suppose you have the following folder:
(Where YYYYMMDD indicates the folder name)
C:\folder\20160101
C:\folder\20160102
C:\folder\20160203
... until the
C:\folder\20160231
Inside the folders are there any file sort by code for example:
XXX0001
XXX0002
XXX0003
I would like to create a batch file that according to three manually set variables
CODE: XXX0001
DATA FROM = for example 20160101
DATE UP = for example 20160131
I want to move all files with the dates entered from Folder Input to Folder Output.
I created a good part of the script, but I'm stuck for the loop.
How can I do?
#echo off
setlocal enabledelayedexpansion
cls
set CODICE=
set /P CODICE=Insert code: %=%
set DATADA=
set /P DATADA=Insert Date[AAAAMMGG es (20160303)]: %=%
set DATAFINE=
set /P DATAFINE=Insert Data [AAAAMMGG es (20160306)]: %=%
set /A DATASOTTR=%DATAFINE%-%DATADA%
FOR /L %%i IN (0,1,%DATASOTTR%) DO (..... AND NOW??)
REM copy /-y \input\%DATA%\%CODICE% c:\output

Related

How to Write a batch script for checking mulitple folders and folder inside a folder and set the csv files to variables?

I have to write a batch script to go inside multiple folders and folder inside a folder and set the CSV files inside it and set it to a variable.
my folder structure is
c:\data\client1\data1.csv
c:\data\client1\data2.csv
c:\data\client1\config\env.csv
c:\data\client2\data1.csv
c:\data\client2\data2.csv
c:\data\client2\config\env.csv
so like these I have many clients folder with config folder inside it and some data CSV's
now I have to use some loops to go inside "c:\data\" and check the client1 folder and inside I need to set var = data1.csv and var = data2.csv using for loop then I need to go inside config folder and set envs= env.csv (i.e the file name or path of the files)
I have tried a code but I am not getting the correct login on how to search and loop inside.
#ECHO OFF & setlocal EnableDelayedExpansion
CD "C:\data"
For /R %%A in (*.csv) DO (
Set "file[!#!]=%%A"
Set /A #+=1
)
For /L %%B in (0,1,!#!) do Echo(!file[%%B]!
I modified the code as per the solution. But now I am unable to set the data1.csv in the client1 folder. and Can anyone explain this code?
Can anyone help me with the logic of the coding part?
output is :
c:\data\client1\data2.csv
c:\data\client1\config\env.csv
c:\data\client2\data1.csv
c:\data\client2\data2.csv
c:\data\client2\config\env.csv
You can do the following to assign them to an indexed variable.
With even moderately large file sets, a singular variable would exceed the line capacity, losing information assigned to it.
#ECHO OFF & setlocal EnableDelayedExpansion
CD "C:\data\client"
For /R %%A in (*.csv) DO (
Set "file[!#!]=%%A"
Set /A #+=1
)
For /L %%B in (0,1,!#!) do Echo(!file[%%B]!

Batch changing titles of mkv files in multiple directories

Ok, I'm total new at this...
Basically I'm using a tool call mkvpropedit to edit the title of my .mkv files
My aim is to create a batch the goes through all sub directories and replace the mkv file titles with their file name.
I have made the following progress...
for %%A in (*.mkv) do "C:\mkvpropedit.exe" "%%A" --edit info --set title="%%A"
Issue with [1]: It works fine but does not affect all sub directories and I would have to use the batch in all the sub directories one by one which will be time consuming.
for /R "C:\whatever" %%I in (*mkv) do "C:\whatever\mkvpropedit.exe" "%%I" --edit info --set title="%%I"
Issue here, It affects all sub directories but my .mkv file titles end up with the entire directory pathway instead of the file name.
Could anyone help me here? Thanks a lot in advance.
BTW if anyone know how to set a long directory pathway into a short form to be use repeated throughout the script (eg. "C:\whatever\whatever...\mkvpropeditexe into mkvpropedit", that would be helpful.
Whether you use %%~nI or %%~nxI (as suggested by Gerhard Barnard) depends on how you want the title: "name" only or "name.extension".
for how to set a long directory pathway into a short form to be use repeated throughout the script; set a variable with the full path\name and use the variable:
set "mkv=C:\whatever\mkvpropedit.exe"
for /R "C:\whatever" %%I in (*.mkv) do "%mkv%" "%%I" --edit info --set title="%%~nI"
Using the help from this thread, here's a little more elaborate batch script I developed:
rem This Bat file will take MKV filenames and apply them to MKV info titles
#echo off
rem Modify next line to path where mkvpropedit.exe !!!!!!!!!
cd "C:\Program Files\MKVToolNix"
set /A errors1=0
rem Modify next line to path where MKV files are. This will also modify MKV's in subdirectories. !!!!!!!!!
for /R "X:\Move" %%X in (*.mkv) DO CALL :loopbody %%X
echo.
echo.
echo Total Errors = %errors1%
echo.
pause
GOTO :EOF
:loopbody
set title0=%*
set "title1=%title0:.mkv=%"
set "title2=%title1:\=" & set "title2=%"
rem The following two lines are to remove additional info I use in the filenames.
set "title3=%title2: 1080p=%"
set "title4=%title3: 720p=%"
set command1=mkvpropedit "%title0%" --edit info --set "title=%title4%"
for /f "delims=" %%a in ('%command1%') do #set response1=%%a
echo %title2%
echo %response1%
echo.
echo.
if /i "%response1:~0,5%"=="Error" (set /A errors1=%errors1% + 1)
GOTO :EOF

Move files into folders in batches of five?

Suppose I have 50 files sorted by name.. I would like to create a batch script which gives me the following result:
Files 1 through 5 -> 01-05
Files 6 through 10 -> 06-10
and so on..How can I create a batch script to achieve this?
Note that 01-05 and 06-10 are directory names..
EDIT: Details
For eg. Consider this:
Source Directory:
101.mp4
102.mp4
103.mp4
104.mp4
and so on..
I want a resulting directory structure like this:
Destination Directory:
101-105:
101.mp4
102.mp4
103.mp4
104.mp4
105.mp4
106-110:
106.mp4
107.mp4
108.mp4
109.mp4
110.mp4
and so on..
This is what you want, change fileCount to change the file number in each subdirectory:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set fileCount=5
set filesNow=0
set nameStart=000
set nameEnd=000
FOR /F "usebackq delims=" %%i IN (`dir /b /O:N *.mp4`) do (
set /a filesNow+=1
set /a tmpValue=filesNow %% fileCount
::echo !filesNow!
::echo !fileCount!
::echo %%i
::echo !tmpValue!
if "!tmpValue!"=="1" (
set "nameStart=%%~ni"
mkdir _tmpDir_
)
move %%~nxi _tmpDir_\
if "!tmpValue!"=="0" (
rename _tmpDir_ !nameStart!-%%~ni
)
set "nameEnd=%%~ni"
)
if exist _tmpDir_ rename _tmpDir_ %nameStart%-%nameEnd%
You need to put them inside a bat/cmd file to work.
filesNow is for file number count.
Basically it's create a tmp folder and move files inside,
When files inside it reach the number, change the folder's name.
Several testing echo command I didn't remove, just used :: to comment them out, you can remove the :: to test them again.

Create a copy of the latest file version, and increase version in filename by one in a windows batch file

I have multiple folders with multiple versioned files in there. For instance filename_v1-xx.xlsx. I'm looking for a way, via batch file, to locate the highest xx number, and create a copy of the file with the filename changed to xx+1.
So if it finds filename_v1-55.xlsx, it needs to create a copy in the same folder called filename_v1-56.xlsx.
I found this: Choose Highest Numbered File - Batch File which allows me to find the highest file number, but I can't work out how to create the new file with the new filename.
What I have so far is:
#echo off
setlocal enabledelayedexpansion
set max=0
for %%x in (Camera-Data-v1-*.xlsx) do (
set "FN=%%~nx"
set "FN=!FN:Camera-Data-v1-=!"
if !FN! GTR !max! set max=!FN!
)
echo highest version: Camera-Data-v1-%max%.xlsx
set newMax=%max%+1
echo Camera-Data-v1-%newMax%.xlsx
pause
This echo's. "highest version: Camera-Data-v1-74.xlsx Camera-Data-v1-74+1.xlsx"
So I think it's the set newMax=%max%+1 which is wrong, I'm using echo's instead of copy for testing, if the echo says the right filename I will change to be copy Camera-Data-v1-%max%.xlsx Camera-Data-v1-%newMax%.xlsx.
The variable number in the filename will always be two characters, sometimes starting with a 0.
Solved, thank you #Aacini works perfectly!
#echo off
setlocal enabledelayedexpansion
set max=0
for %%x in (Camera-Data-v1-*.xlsx) do (
set "FN=%%~nx"
set "FN=!FN:Camera-Data-v1-=!"
if !FN! GTR !max! set max=!FN!
)
echo highest version: Camera-Data-v1-%max%.xlsx
set /A newMax=%max%+1
copy Camera-Data-v1-%max%.xlsx Camera-Data-v1-%newMax%.xlsx
pause

Batch - display folder names as options

I'm trying to create a small batch file which reads a folder (path is set as a variable in the file). It should display the names of all sub-folders as choices for the user and when the user chooses one that folder name should be saved in a variable for later use. The idea is that I have alot of branches I'm working on and in all of them there is a little jar file I want to run with this batch. So the batch present me a list of all branches in the folder and when I pick on it will start the jar file located in that branch folder.
EXAMPLE:
C:\code
contains
C:\code\branch1
C:\code\branch2
C:\code\branch3
Then I want the batch to present the following menu to the user:
1. branch1
2. branch2
3. branch3
When the user has chosen the folder name (f.ex. branch2) is saved in a variable for later use.
I've tried alot of googling, but nothing helpful came up. Sofar I've managed to read the sub-folders' names, but I dont know where to go from here.. can anyone point me in the right direction?
We first need delayed expansion
setlocal enabledelayedexpansion
Then need a list of all subfolders (assuming %dir% being set to the directory you want subfolders of):
set Index=1
for /d %%D in (%dir%\*) do (
set "Subfolders[!Index!]=%%D"
set /a Index+=1
)
set /a UBound=Index-1
Then you can present a choice (I added a little input validation, but it's not enough):
for /l %%i in (1,1,%UBound%) do echo %%i. !Subfolders[%%i]!
:choiceloop
set /p Choice=Your choice:
if "%Choice%"=="" goto chioceloop
if %Choice% LSS 1 goto choiceloop
if %Choice% GTR %UBound% goto choiceloop
Then you can set a variable with the subfolder the user chose:
set Subfolder=!Subfolders[%Choice%]!

Resources