|
Answer» Hey all, i have a PRETTY basic batch file scripting question.
i have a bat file and i need to remove a part of it, preferebly automatically with another bat file. here is the before and after i would like (this is just the one line i need edited from a batch file, not the whole batch)
entry in file1.bat:
net use \\uncpathtoserver /user:[emailprotected] *
desired outcome after RUNNING batch file:
net use \\uncpathtoserver /user:example *
Basically i am trying to remove the @domain.com part. I have been USING an older version of samba that let me use full email address for my username, and the newer versions of samba dont like it, so i need to change that in a number of user's scripts. We are not on an active directory system, which is why i have the scripts in the first place.
thanks in advance for the help.Why not just copy all the scripts to another directory, then edit all of those. Yes, you could automate the process, but why? If a string has exactly one @ character, you could remove it by splitting the string into tokens using @ as the delimiter.
This is before.bat
@echo off REM Comment 1 net use \\uncpathtoserver1 /user:[emailprotected] * REM Comment 2 net use \\uncpathtoserver2 /user:[emailprotected] * REM Comment 3 net use \\uncpathtoserver3 /user:[emailprotected] * REM Comment 4 net use \\uncpathtoserver4 /user:[emailprotected] *
This script I call transform.bat. It assumes
(1) The script to be transformed has @echo off as the first line. That line will be skipped and a new one output at the start of processing. (2) Only lines STARTING "net use" will have @ symbols
@echo off echo @echo off for /f "skip=1 usebackq tokens=1* [emailprotected]" %%A in ("%1") do echo %%A | find /i "net use">nul && (echo %%A *) || (echo %%A)
usage:
transform.bat "before.bat" > "after.bat"
after.bat
@echo off REM Comment 1 net use \\uncpathtoserver1 /user:user1 * REM Comment 2 net use \\uncpathtoserver2 /user:user2 * REM Comment 3 net use \\uncpathtoserver3 /user:user3 * REM Comment 4 net use \\uncpathtoserver4 /user:user4 *
Salmon, Worked Like a Charm!
Thanks a tonok, maybe not like a charm. I noticed a few issues with the output. maybe if i put the whole script it will be easier to see
orig script:
@echo off color b0 title Map Network Drives Script net use * /delete rem Map network drives cls echo Please enter your E-MAIL password net use \\uncpathtoserver /user:[emailprotected] * net use T: \\uncpathtoserver\share1 /persistent:yes net use U: \\uncpathtoserver\share2 /persistent:yes net use n: \\uncpathtoserver\share3 /persistent:yes rem Show drives cls net use pause Press any key to continue . . . .
When i run the script i get the FOLLOWING:
@echo off color b0 title Map Network Drives Script net use * /delete * rem Map network drives cls echo Please enter your E-MAIL password net use \\uncpathtoserver /user:username * net use T: \\uncpathtoserver\share1 /persistent:yes * net use U: \\uncpathtoserver\share2 /persistent:yes * net use n: \\uncpathtoserver\share3 /persistent:yes * rem Show drives cls net use * pause Press any key to continue . . . .
When what i need to be getting is:
@echo off color b0 title Map Network Drives Script net use * /delete rem Map network drives cls echo Please enter your E-MAIL password net use \\uncpathtoserver /user:username * net use T: \\uncpathtoserver\share1 /persistent:yes net use U: \\uncpathtoserver\share2 /persistent:yes net use n: \\uncpathtoserver\share3 /persistent:yes rem Show drives cls net use pause Press any key to continue . . . .
Notice the extra *,s and spaces?Try this
@echo off echo @echo off for /f "skip=1 usebackq tokens=1* [emailprotected]" %%A in ("%1") do echo %%A | find /i "net use \\">nul && (echo %%A *) || (echo %%A)
Result
@echo off color b0 title Map Network Drives Script net use * /delete rem Map network drives cls echo Please enter your E-MAIL password net use \\uncpathtoserver /user:username * net use T: \\uncpathtoserver\share1 /persistent:yes net use U: \\uncpathtoserver\share2 /persistent:yes net use n: \\uncpathtoserver\share3 /persistent:yes rem Show drives cls net use pause Press any key to continue . . . .
Success!
Thanks a ton
|