Batch file to generate n amount of folders - windows

Hi I would like to create a batch file which makes n amount of folders and moves them to a specified dir. something like make folders {00001-00009} to put it bluntly. I don't want to manually put each number in and I would like to be able to exclude numbers as well.
so far I have tried this code below. I get the 5 folders made in C:\source\
and I am able to dictate how they are named to an extent, by changing j=5 to whatever integer, however sometimes the file makes folders from 00256-003372 instead of what I specified. this happens when I change j to j=256 and run the program.
echo off
cd C:\source\ :: I included this because for some reason folder 1 copied to root
SET /a j=1
:floop
IF %j%==5 GOTO END
md 00%j%
cd 00%j%
SET /a j=%j%+1
cd C:\source\
GOTO FLOOP
:end
The other way I did this was to manually input each number in a .bat file such as below. which works fine but I have to make a line for each folder.
md C:\Device_Numbers_Folder\00001
md C:\Device_Numbers_Folder\00002
P.S I named the dir file source because I am running a bunch of .bat files with CALL and this is the source part.

I think you want something like this:
#echo off
setlocal EnableDelayedExpansion
::CD "C:\source\"
::only change these three lines
set "start=1"
set "amount=5"
set "length=5"
set "exclude=4,5"
set "excluded=0"
FOR %%G IN (%exclude%) DO (set /a "excluded+=1")
set /a "last=%start%+%amount%+%excluded%"
for /l %%i in (%start%,1,%last%) do (
set "skip=0"
FOR %%G IN (%exclude%) DO (if "%%i"=="%%G" set "skip=1")
if !skip! equ 0 (
set "folderName=0000000000%%i"
set "folderName=!folderName:~-%length%!"
md "!folderName!"
)
)
In this start is the first number, amount is the amount of folder it needs to create, and length is the lenght of the names of the new files, so 1 with length 5 becomes 00001. Exclude is an comma seperated list of all the numbers not allowed

Related

How could i copy multiple files 1 at a time from 2 folders to another folder with a windows batch file or vbs script

I am working with a video camera and we have a program that displays the saved videos from an SD card when inserted into a PC. At some point the manufacturer of the camera changed the directory structure and naming convention for the saved files. I would like to create a batch file or VBS Script that will reorganize the files into the old structure. This will be a quick and dirty fix for windows based PC's and until we can re-write the software which will include support for MAC's. It can be a batch file or a VBS Script but must run under a Windows command prompt with no additional software installed. The camera has front and rear cameras so there are 2 files to deal with and there could be 1 or more video captures to relocate.
The number of folders would depend on the number of videos saved, let's say there are 4 videos saved so the original structure looked like this.
- video1
- video.TS
- video2.TS
- video2
- video.TS
- video2.TS
- video3
- video.TS
- video2.TS
- video4
- video.TS
- video2.TS
The new structure looks like this
- Normal
- F
- DATETIME-000001F.TS
- DATETIME-000002F.TS
- DATETIME-000003F.TS
- DATETIME-000004F.TS
- R
- DATETIME-000001R.TS
- DATETIME-000002R.TS
- DATETIME-000003R.TS
- DATETIME-000004R.TS
The object is to move these files into the older file structure so the software can read and display them. I already have a batch file that runs when the SD card is inserted so my assumption is that I can include some script before the normal process fires to move these files around. I am pretty rusty with scripting and need some guidance.
My current script look like this.
setlocal enableextensions enabledelayedexpansion
set count=0
for %%x in (\Normal\F\*.TS) do (
set /a count += 1
mkdir video!count!
move /Y \Normal\F\*.TS \video!count!\video.TS
move /Y \Normal\R\*.TS \video!count!\video2.TS
)
endlocal
There are always 2 videos, 1 for the front camera and 1 for the rear camera so I am only using the "F" directory to get the count.
Without the move commands it creates the directory structure just fine... If there is 1 file it only creates 1 folder, if there are 8 files it creates 8 folders. but when there are multiple files it wants to put all of the files in the first folder.
I assume I would need to nest another loop but everything I have tried has failed and this is the closest attempt.
#ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\normal\F\*-*.ts" '
) DO (
SET "video2=%%~na"
SET "video2=!video2:~0,-1!R%%~xa"
FOR /f "tokens=2delims=.-" %%i IN ("%%a") DO (
SET "video=%%i"
SET /a video=1!video:~0,-1!-1000000
MD "%destdir%\video!video!" 2>NUL
MOVE /y "%sourcedir%\normal\F\%%a" "%destdir%\video!video!\video.TS" >NUL 2>nul
MOVE /y "%sourcedir%\normal\R\!video2!" "%destdir%\video!video!\video2.TS" >NUL 2>nul
)
)
)
GOTO :EOF
You would need to change the values assigned to sourcedir and destdir to suit your circumstances. The listing uses a setting that suits my system.
I deliberately include spaces in names to ensure that the spaces are processed correctly.
First, assign to %%a each filename found in the ...\F directory.
Then construct the corresponding filename in ...\R by taking the name part of %%a, removing the last character and replacing it with R, then appending the extension from %%a.
Next, derive the sequence number from %%a by selecting the second token from %%a using - and . as delimiters. Set the number part of the destination directoryname by prefixing all bar the last character of video with 1 and subtracting 1000000 to convert it to the natural form.
Create the destination directory and move in and rename the source files
[edit in response to comments]
#ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "drives=d e f g h i j k l m n o p q r s t u v w x y z"
SET "sourcedir=normal"
:: locate drive that has directory "?:\normal\f"
FOR %%d IN (%drives%) DO IF EXIST "%%d:\%sourcedir%\f\." SET "sourcedir=%%d:\%sourcedir%"&SET "destdir=%%d:\video"&GOTO founddrive
ECHO Could not find "%sourcedir%\f" on any drive
GOTO :eof
:founddrive
SET /a destnum=1
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\f\*-*.ts" '
) DO (
MD "%destdir%!destnum!" 2>NUL
SET "filename=%%~na"
set "filename=!filename:~0,-1!R%%~xa"
MOVE /y "%sourcedir%\F\%%a" "%destdir%!destnum!\video.TS" >NUL 2>nul
MOVE /y "%sourcedir%\R\!filename!" "%destdir%!destnum!\video2.TS" >NUL 2>nul
SET /a destnum+=1
)
GOTO :EOF
OK, so this version first scans for the directoryname, then picks the files and places them, with namechanges, in directory video1... on the same drive.

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]!

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.

How to find the new files after a specific time point in windows ?(command line)

I know there is a way in linux to filter all file generated after a specific time.
But how could we do that in windows command line ? Or in bash .
For example, I have three files in a folder. After 10/10/2016, 12:12:54, a new files was generated into this folder and I need to know the new file's name,size and path.
Or,
I don't know when the new files will be generated. I want to check each 10 mins. If there are some new files generated after a specific , I can get the file's name, path and size.
I search something about that , I know I can use forfiles /P directory /S /D +08/01/2013 to do that. But it will displays all the files which are modified after 08/01/2013 under directory. But I want it displays the folders in directory and all files in directory folder(not in its sub directory).
Although you did not show any own efforts to solve your task, I decided to provide a script that returns a list of files created since the previous execution. It does not check the file creation time stamp, because date/time maths is not natively supported in pure batch-file solutions. Instead it generates a list of files, stores it in a temporary file and compares it with a previously saved list.
Opposed to relying on file time stamps, this will for sure recognise every new files. When checking the time stamps, files may be considered as new since the last run erroneously or new files may not be recognised erroneously, particularly such files that are created during execution of the script.
So here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "TARGET=D:\Data" & rem // (path to the directory to observe)
set "PATTERN=*.*" & rem // (search pattern for matching files)
set "LIST=%TEMP%\%~n0" & rem // (file base name of the list files)
set "FIRSTALL=#" & rem /* (defines behaviour upon first run:
rem set to anything to return all files;
rem set to empty to return no files) */
rem /* Determine which list file exists, ensure there is only one,
rem then toggle between file name extensions `.one`/`.two`: */
set "LISTOLD=%LIST%.two"
set "LISTNEW=%LIST%.one"
if exist "%LIST%.one" (
if not exist "%LIST%.two" (
set "LISTOLD=%LIST%.one"
set "LISTNEW=%LIST%.two"
) else (
erase "%LIST%.one"
if defined FIRSTALL (
> "%LIST%.two" rem/
) else (
erase "%LIST%.two"
)
)
) else (
if not exist "%LIST%.two" (
if defined FIRSTALL (
> "%LIST%.two" rem/
)
)
)
rem /* Create new list file, containing list of matching files
rem sorted by creation date in ascending order: */
> "%LISTNEW%" dir /B /A:-D /O:D /T:C "%TARGET%\%PATTERN%"
if not exist "%LISTOLD%" (
> nul 2>&1 copy /Y "%LISTNEW%" "%LISTOLD%"
)
rem // Search new list file for items not present in old one:
2> nul findstr /V /I /X /L /G:"%LISTOLD%" "%LISTNEW%"
if ErrorLevel 2 type "%LISTNEW%"
rem // Delete old list file:
erase "%LISTOLD%"
endlocal
exit /B
The first time the script runs, all files in the monitored directory are returned, unless you change set "FIRSTALL=#" to set "FIRSTALL=", in which case no files are returned the first time.
The core command is findstr which is configured so that the old list file provides literal search strings for being used to search the new list file and to return not matching lines, so the output are those lines of the new list file which do not occur in the old one.
Supposing the script is saved as new-files-since-last-run.bat, you could wrap around another tiny script that constitutes an endless loop with a polling rate of 10 minutes, like this:
#echo off
:LOOP
> nul timeout /T 600 /NOBREAK
call "%~dp0new-files-since-last-run.bat"
goto :LOOP
Setting up Windows Task Scheduler might be a better option though.

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