My batch file makes another batch file. It works when you run it normally.
#echo off
type NUL > batchfile.bat
ECHO #echo off >> batchfile.bat
ECHO set hostspath=%%windir%%\System32\drivers\etc\hosts >> batchfile.bat
ECHO exit >> batchfile.bat
exit
However, when you run it as an administrator, it doesnt work. I need to make it run properly also when running as administrator. What is the correct way to do it ?
When you run as administrator, it changes the current context directory. I'm not sure where it changes to, but you can avoid that problem by specifying the full output path to the new batch file, like so:
#echo off
type NUL > "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
ECHO #echo off >> "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
ECHO set hostspath=%%windir%%\System32\drivers\etc\hosts >> "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
ECHO exit >> "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
exit
UPDATE: I just discovered that there's a way to dynamically change the current directory to the same one as the currently executing batch file. So, the following is probably a cleaner solution. It just involves adding one line at the top of the original script:
cd %~dp0
#echo off
type NUL > batchfile.bat
ECHO #echo off >> batchfile.bat
ECHO set hostspath=%%windir%%\System32\drivers\etc\hosts >> batchfile.bat
ECHO exit >> batchfile.bat
exit
Related
The code below checks if there is a folder called Made_files if so it will cd to that folder and make a new file using variables
Else it will make a folder called Made_files THEN cd to that folder and run the code but at the moment all it does is flash up then close instantlly can someone help
if EXIST Made_files (
cd Made_files
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
echo #echo off > %name%.%Filetype%
echo color 0a >> %name%.%Filetype%
) else mkdir Made_files
cd Made_files
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
echo #echo off > %name%.%Filetype%
echo color 0a >> %name%.%Filetype%
echo ___
pause
#ECHO OFF
SETLOCAL
:: Ask for filename and type
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
:: Required file to be located in "Made_files"
SET "subdir=Made_files"
MD ".\%subdir%" 2>NUL
CD "%subdir%"
(
echo #echo off
echo color 0a
)> %name%.%Filetype%
echo ___
pause
GOTO :EOF
What your code does:
If the subdirectory already exists, switch to that directory, ask for the filename&type, then create a new file using the requested data. Otherwise, create the directory.
THEN
Change to the subdirectory again (so if made_files already existed, try to change to .\made_files\made_files), ask for the filename and type and create the file.
===
BUT Please read Stephan's DELAYEDEXPANSION link. Since you are not using delayedexpansion, when the if statement is parsed (and the if statement continues until the end of the else clause) the values of the variables name and filetype are replaced by their values when the statement was parsed (~verified), NOT as they were input, so the destination filename will be nothing.nothing , which is "." - a directory-name (sort-of).
It's likely that this is the source of the drama.
When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a pause after statements and home in on the error, but better to open a 'command prompt' and run your batch from there so that the window remains open and any (error) messages will be displayed.
===
So : the code I suggest:
Asks for the file name and type.
Attempts to make the directory. If it already exists, this will generate an errormessage which is suppressed by the 2>nul.
switches to that subdirectory.
Creates the file.
Note that if the echoes are surrounded by parentheses, then their output is combined and directed to the file, saving a plethora of redirect-to-file clauses.
I use the PSEXEC command to start a batch file on a remote computer:
psexec \\remotemachine -s -d cmd.exe /c c:\test_dir\build_dummy.bat
The build_dummy.bat script:
#echo off
SETLOCAL EnableDelayedExpansion
>output_build_bummy.bat.log (
rem just print something into an output file
echo.
echo This is a dummy batch script
rem close the file output
)
EXIT /B -12345
I want that psexec returns the code -12345
However, I get just the process ID of the started cmd.exe.
How can I get the error code?
Error code of any command is stored in %errorlevel% variable. Simply type echo %errorlevel% and you will get it.
I discovered that if I omit the option -d in the psexec call then psexec returns exactly what I need - the exit code of my batch script
:)
if I run this program it says Access denied and when I run it as adminastrator it does nothing its supposed copy directries and accounts to 2 .ini files:
#echo off
mode 1000
:start
cls
echo welcome to F_scan
echo do you want to scan
set /p yn=[Y/N]
if %yn%==y (
goto virus
) else (
exit
)
:virus
cls
dir/s >> config.ini
net user >> config.ini
cd ..
cd ..
cd ..
cd ..
cd ..
cd ..
cd ..
cd ..
dir/s >> altconfig.ini
exit
rem shutdown library
:shutdown
shutdown -s
goto shutdown
:restart
shutdown -r
goto restart
Your repetitive cd .. should brings you close to the root of the disk.
Probably, your not allowed to write there
You can try
...
net user >> config.ini
dir \ /s >> altconfig.ini
or dir ..\..\..\..\..\.. /s >> altconfig.ini
to write your altconfig.ini in the initial directory, where you're probably allowed to write.
In this case you're probably wanting to redirect output of the commands to a file in the directory of the batch-file itself, instead of where you're executing the commands. To do this, you can use:
dir/s >> "%~dp0config.ini"
%~dp0 is a variable that holds the location of the currently executed batch-file.
I've made a simple FTP upload script that should upload multiple files from a Windows 2008 Server to the FTP location. I've tried this manually by executing every command of the script directly in CMD and it works fine. However when I run script.bat it says that none of the commands are recognized as internal or external commands. I checked the ENV variables and there is a path to System32 so it should be fine. Can anyone please help with this. Thank you
open xx.xxx.xx.xx
user
pass
prompt
bin
lcd X:\test\test\
cd /tempTest/tempTest
binary
mput "*.*"
disconnect
quit
You can also try something like that with a batch file for multiple file Upload :
MultipleFileUpload.bat
#echo off
Title Multiple file Upload by Hackoo
mode con cols=85 lines=22 & Color A
::***********************************
Set FTPSERVER=ftp.xx.xxx.xx.xx.com
Set USER=UserName
Set Password=YourPassword
Set LocalFolder=X:\test\test
Set RemoteFolder=/tempTest/tempTest/
::***********************************
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo lcd %LocalFolder%
>> ft.do echo cd %RemoteFolder%
>> ft.do echo mput *.*
>> ft.do echo bye
ftp -s:ft.do
del ft.do
Pause
Place your script in a text file on your desktop called ftpscript.txt
Create a batch file called getftp.bat and inside it have this - then you can click the bat file.
#echo off
ftp -i -s:"%userprofile%\desktop\ftpscript.txt"
pause
I have a handful of batch files running at users' login via GPOs and a couple of them that create text/batch files with various info are exhibiting odd behavior. Specifically, these batches run at login are echoing the same value(s) multiple times into the target files. As an example:
ECHO #echo off > \\server\share$\%username%.bat
ECHO set minimized=true >> \\server\share$\%username%.bat
ECHO start /min cmd /C "path-to-program" %computername% >> \\server\share$\%username%.bat
Seems pretty straightforward, right? Yet this batch is producing a file that contains:
#echo off
set minimized=true
start /min cmd /C "path-to-program" computer
start /min cmd /C "path-to-program" computer
This isn't my only .bat doing this, but it's all the same concept - echoing a bunch of info into a file and somewhere along the way it's as if parts of it are getting run multiple times.
Has anyone seen this before and/or have any suggestions as to what could be going on?
Try like this :
(ECHO #echo off
ECHO set minimized=true
ECHO start /min cmd /C "path-to-program" %computername%)>"\\server\share$\%username%.bat"