Home > Net >  Copying a folder name to a variable in batch
Copying a folder name to a variable in batch

Time:01-29

I have a folder at this location:

E:\Scripts\TEMPLATE\REC_DRAWER\1212314231_001001000

I would like using a batch script to copy that folder name into a variable excluding the _001001000 the 0 and 1 is randomized but is always the same length of characters.

I want to have a variable named folder_name with the following string content:

1212314231

I cant really wrap my head around how I can approach this,

CodePudding user response:

You can use the for command in a batch script to iterate through the characters in the folder name and store the characters that come before the _001001000 in a variable. Here is an example of how you might do this:

set folder_path=E:\Scripts\TEMPLATE\REC_DRAWER\1212314231_001001000
set folder_name=

for /f "tokens=1 delims=_" %%a in ("%folder_path%") do set folder_name=%%a

echo %folder_name%

This script will first set the variable folder_path to the path of the folder you provided. Then, it uses the for command to iterate through the characters in folder_path, using the _ as the delimiter. The tokens=1 option tells the for command to only store the first token (i.e., the characters before the _) in the variable folder_name. Finally, the script will echo the value of folder_name which should be 1212314231

  • Related