i am currently getting these errors when running my batch file:
System error 1379 has occurred.
The specified local group already exists.
Press any key to continue . . .
The system cannot find the file users.csv. {This is where my problem lies}
Press any key to continue . . .
There is no such global user or group: users.csv
My Batch File
#echo off
REM Adding a local group
net localgroup NEWDOSGROUP /Comment:"New Group Assignment" /add
pause
REM Add all the users from a .csv file
FOR /F "tokens=1,2 delims=," %%G IN (users.csv) DO (
net adduser
pause
REM Adding users to a local group
FOR /F "tokens=1,2 delims=," %%a IN ("users.csv") Do (
net localgroup NEWDOSGROUP %%a /add
)
pause
any help would be amazing thanks.
Put users.csv in the same folder as the batch file.
This loop is also missing a closing parenthesis by the look of it, and the net adduser should probably have an argument of %%G or %%H or similar
FOR /F "tokens=1,2 delims=," %%G IN (users.csv) DO (
net adduser
REM Add all the users from a .csv file
FOR /F "tokens=1,2 delims=," %%G IN (users.csv) DO (
net adduser
REM ARE YOU MISSING A CLOSE-PARENTHESIS HERE?
REM WHAT IS NET ADDUSER?
REM WHY ARE YOU NOT USING %%G,%%H?
REM HERE YOU'VE OPENED USERS.CSV... ARE YOU THEN TRYING TO RE-OPEN IT?
REM IS AN OPEN USERS.CSV CAUSING THE PROBLEM?
REM IS THE SYNTAX OF THE FOR...%%a CORRECT? USEBACKQ PERHAPS?
REM NOT SHOUTING - JUST EMPHASISING
pause
REM Adding users to a local group
FOR /F "tokens=1,2 delims=," %%a IN ("users.csv") Do (
net localgroup NEWDOSGROUP %%a /add
)
pause
Related
i have a batch script which i use to merge all the same name txt files from directories & subdirectories into one, here's my code:
#echo off
for /f "tokens=*" %%a in (textfilenames.txt) do (
for /D /R %%I in (%%a.txt) do (
type "%%I" >> merged.tmp
echo. >> merged.tmp
)
ren merged.tmp All_Combined_%%a.txt
)
)
#pause
and so when the loop doesn't finds the file on some directories then this msg is displayed:
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
Press any key to continue . . .
and i wanna hide above error and so i used >NUL in file name eg:
#echo off
for /f "tokens=*" %%a in (textfilenames.txt) do (
for /D /R %%I in ('%%a.txt^>NUL') do (
type "%%I" >> merged.tmp
echo. >> merged.tmp
)
ren merged.tmp All_Combined_%%a.txt
)
)
#pause
but i'm still getting error msg, i want to make this script completely silent like no error nothing or if somehow it's not possible then i wann customize the error to something like:
The system cannot find the example.txt specified. in the \Subfolder\Enigma\
etc!
If you do it right, you do not need to hide anything.
In this example we use the dir command with the /s function to search for files. It will not complain about files not found because it does not expect the file exist indefinitely in any given directory, it simply searches for it:
#echo off
for /f "useback delims=" %%a in ("textfilenames.txt") do (
for /f "delims=" %%I in ('dir /b/s/a-d "%%a.txt"') do (
(type "%%I"
echo()>>All_Combined_%%a.txt
)
)
)
#pause
Note, I eliminated the ren part as that is not needed. You can write to the combined file in the loop.
I also use echo( instead of echo. for reasons that can be found on numerous answers in SO.
Lastly, we can eliminate one parenthesized code block, by putting the second for loop inline with the first:
#echo off
for /f "useback delims=" %%a in ("textfilenames.txt") do for /f "delims=" %%I in ('dir /b/s/a-d "%%a.txt"') do (
(type "%%I"
echo()>>All_Combined_%%a.txt
)
)
#pause
I am trying to write a batch script, which removes all non pdf files from a directory.
The directory name is OUTPUT/
this what i got
FOR /f %%f IN (.\OUTPUT) DO
SET fileName=%%f
IF NOT "!fileName:~-3!"=="pdf" (
DEL !fileName!
)
Although your question is simply that you're not using the correct syntax with your for command, there are other remarks I noted, which I decided would be better as an answer.
The most obvious one is that there is that you're setting a variable for no reason, and then having to use it with delayed expansion.
To do it as you were, it should look more like this:
#Echo Off
SetLocal DisableDelayedExpansion
For %%G In (Output\*) Do (
Set "fileName=%%G"
SetLocal EnableDelayedExpansion
If /I Not "!fileName:~-4!"==".pdf" (
Del /F "!fileName!"
)
EndLocal
)
These entire batch files should do the same thing.
Standard for loop:
#For %%G In (Output\*)Do #If /I Not "%~xG"==".pdf" Del /F "%%G"
Using the for /f variant:
#For /F "Delims=" %%G In ('Dir /B/A-D-S-L "Output" 2^>NUL^|FindStr /VILE ".pdf"')Do #Del /F "Output\%%G"
or over multiple lines, if you find it easier to read:
#Echo Off
For /F "Delims=" %%G In (
'Dir /B/A-D-S-L "Output" 2^>NUL^|FindStr /VILE ".pdf"'
) Do (
Del /F "Output\%%G"
)
Please remember that almost every windows cli command has built-in help, and the vast majority accept the /? option, e.g. for /?, del /?. Additionally you can use the help command, e.g. help dir, help findstr.
I need to include page size information of many single-paged pdf's into their filenames. E.g. "150x250mm.pdf". I found no renamer apps able to do it. I suspect this could be done using a batch file and pdfinfo.exe (from xpdf suite), but I have no idea how to make use of it.. Could you give me some hints?
Yes, you can convert from postscript points to MM. In this case, the script is in the top level folder containing the PDF's to be renamed. It does go into subfolders. If you don't want or need that, remove the /s from the dir command on the 5th line. Change the paths as needed.
#echo off
setlocal enabledelayedexpansion
set "pdfi=U:\Scripts\Utilities\xpdf\pdfinfo.exe"
for /f "delims=" %%a in ('dir /b /s *.pdf') do (
for /f "tokens=3,5 delims= " %%b in (
'%pdfi% -meta "%%a"^|find /i "Page size:"') do (
set pts=%%b %%c
for %%d in (!pts!) do (
call :Eval %%d*.352777778 mm
set "mm1=!mm1!x!mm!"
)
ren "%%~dpfnxa" "!mm1:~1!.pdf"
set mm1=
)
)
exit /b
:Eval <in> <out>
setlocal
if exist eval.vbs del eval.vbs
>eval.vbs echo wsh.echo formatnumber(eval("%1"),0)
for /f "delims=" %%a in (
'cscript //nologo eval.vbs'
) do endlocal & set %~2=%%a
del eval.vbs
I have a local server on my Windows XP Virtual Box.
Every time my main network changes like from wifi to cable or I move to a different network the IP of my Win XP box changes.
I want to run a batch file to map the new IP to some common name in my hosts file
C:\WINDOWS\system32\drivers\etc\hosts
for example if I have now:
10.97.100.74 www.myAppServer.com
and if the ip changes to 192.168.11.9,
I want to search for the string myAppServer in the hosts file and replace the whole line with
192.168.11.9 www.myAppServer.com
I was able to get the current IP using:
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
set IP=%IP%
but how do I find that line and replace it?
I found this on DOS Tips
BatchSubstitute - parses a File line by line and replaces a substring
#echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
::BatchSubstitute - parses a File line by line and replaces a substring
::syntax: BatchSubstitute.bat OldStr NewStr File
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
::$changed 20100115
::$source http://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)
If you want to replace just one line, you can do it this way:
#echo off
setlocal
cd "C:\WINDOWS\system32\drivers\etc"
for /F "delims=:" %%a in ('findstr /N "myAppServer" hosts') do set lineNum=%%a
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" hosts') do (
if %%a equ %lineNum% (
echo 192.168.11.9 www.myAppServer.com
) else (
echo %%a
)
)) > hosts.new
This program eliminate empty lines and will have problems if the hosts file contain Batch special characters, like | > < & !. These details may be fixed, if needed.
The simplest way of doing it is not replacing it. Remove it and add a new line.
If you have your ip address in %IP%, then
set "hostsFile=%systemroot%\system32\drivers\etc\hosts"
(
findstr /v /i /c:"www.MyAppServer.com" "%hostsFile%"
echo(
echo %IP% www.MyAppServer.com
) > "%temp%\hosts"
move /y "%temp%\hosts" "%hostsFile%" >nul 2>nul
And be sure you have rights enough to change the hosts file and that no antivirus is blocking changes to it.
this get the job done finally:
#echo off
REM Set a variable for the Windows hosts file location
set "hostpath=%systemroot%\system32\drivers\etc"
REM set "hostpath=C:\projectStar\Programs\etc"
set "hostfile=hosts"
REM Make the hosts file writable
attrib -r -s -h "%hostpath%\%hostfile%"
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
REM echo %IP%
set IP=%IP%
move /y "%hostpath%\%hostfile%" "Hs.txt"
findstr /v/c:"www.nexsoftnetwork.com" Hs.txt > "Hss2.txt"
echo %IP% www.nexsoftnetwork.com >>"Hss2.txt"
move /y "Hss2.txt" "%hostpath%\%hostfile%"
echo Done.
pause
thank you all for your inputs.
But I wanted to hit this server from my phone which will be on the same network as the server. and eventually I came to notice that unless I modify the hosts file of the host operating system or the mobile phone it won't still solve my problem. this solution works only if I want to hit this server with in the virtual box.
I have a pretty basic script that echos local administrator accounts. My goal is to get rid of all of the header/footer information.
So far I have:
FOR /F "skip=6" %%G IN ('net localgroup administrators') DO echo %%G
Which echos:
Administrator
MyName
The
"The" being the first word in the footer: "The command completed successfully."
So I'd like to get rid of "The" but I understand that I may have to restructure the entire script, which is fine. I have tried saving to a variable %str% but you can't set multi-line variables. Also, using a txt file as a buffer is not an option.
Any input?
I can think of two simple solutions:
FOR /F "skip=6" %%G IN ('net localgroup administrators') DO if %%G neq The echo %%G
or
FOR /F "skip=6" %%G IN ('net localgroup administrators ^| findstr /vb The') DO echo %%G
I suppose one could argue a user name could be "The", in which case you can be more precise with the filter:
FOR /F "skip=6" %%G IN ('net localgroup administrators ^| findstr /vc:"The command completed successfully."') DO echo %%G