mcneilyo
mcneilyo

Reputation: 95

Batch file variable filenames and editing

Fairly new to the game and have spent many hours looking for help and examples. I've had a version of this working, although I want to streamline the process by having a single input file 'computer_listc.txt'. I basically want to read in a series of folders paths (from computer_listc.txt), and use parts of the path to write further cmd expressions.

My problem is that fname and comp_id are not being set properly. I want to make them equal to T (the filename), and then edit them.

fname needs to be the filepath with the first 12 characters removed. While comp_id needs to equal the computername specified in the filepath e.g.

fname = c_modelling\model\slave1
comp_id = VOSTRO460-1

Heres the script. Can you see my error??

for /f "usebackq tokens=*" %%T in ("C:\c_modelling\Model\cal21_cal\Computer_ListC.TXT") do (
set fname = %%T
ren fname "%fname%" "C:\%fname%:~12%"
set comp_id = %%T
%comp_id% = %{comp_id:0:12} 
mkdir %%T > NUL 2>&1
echo cd %fname% >\\%%T\beo_insC.cmd
echo beo >>\\%%T\beo_insC.cmd
robocopy c:\c_modelling\model\cal21_cal \\%%T\ /s /e
start cmd.exe /c psexec \\%comp_id% -c c:\c_modelling\model\cal21_cal\beo_insC.cmd
del \\%%T\beo_insC.cmd
)

Computer_ListC.txt contains the following file names: This will eventually have a long list of computer folder paths.

VOSTRO460-1\c_modelling\model\slave1
VOSTRO460-1\c_modelling\model\slave2

I'm creating a *.cmd file for use with 'psexec', as when I direct the process to a local computer, the path defaults to \system32.

Any assistance would be very helpful!!

Upvotes: 2

Views: 9308

Answers (1)

Magoo
Magoo

Reputation: 79983

First issue is that SPACES in string SET statements are significant. The variablename AND the value assigned will both include any spaces, so set fname = %%T will set a variable named "fname ", not "fname" - and its value will contain the leading space before the value of %%T

Next is the perpetual delayed-expansion issue. Within a block (a sequence of parenthesised statements) any %var% will be replaced by its value at PARSE time - before execution of the loop begins.

To access the RUN-TIME value, you need to execute a SETLOCAL ENABLEDELAYEDEXPANSION statement, and while delayedexpansion is invoked, !var! accesses the RUN-time variable value.

Note however that you cannot simply invoke delayedexpansion within a loop with impunity. There is a limit to the number you can have open at any one time, so you need to 'close the bracket' with an ENDLOCAL command (reaching logical End-Of-File is equivalent to an ENDLOCAL) and the downside is that ENDLOCAL restores the environment to its value when the matching SETLOCAL was invoked. For this reason, SETLOCAL ENABLEDELAYEDEXPANSION is normally executed at the start of the batch, usually after the @echo off

Next issue is

ren fname "%fname%" "C:\%fname%:~12%"

The rename command's syntax is ren sourcename newname - exactly two arguments, enclose any argument containing spaces in "rabbit's ears" Further, the destination name is a NAME only - not a path.

Then there's this:

%comp_id% = %{comp_id:0:12} 

Assuming you've fixed this to remove the spaces in variablenames and that comp_id has been assigned the value fred and you've realised that you are dealing with the RUN-time value of comp_id then this would now appear as

!comp_id!=%{comp_id:0:12} 

which Batch would interpret as

fred=%{comp_id:0:12} 

and promptly give up.

What you are probably expecting to do is

set comp_id=!comp_id:~0,12! 

that is, set comp_id to the RUN-time value of comp_id, from the first character (0) for 12 characters.

Upvotes: 4

Related Questions