I keep getting access denied or nothing at all - batch-file

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.

Related

Batch file console not stopping to display the output

I'm trying to execute a batch file but its console is not stopping for its output.
here's the code:
goto D:
cd postgresql-9.6.0-1-windows-x64-binaries
cd pgsql
cd bin
start pg_ctl start -D D:\pg_data\data
#echo on
echo server started
pause
I am assuming it is on the D: drive. use cd /d see cd /? for the reason why.
#echo off
cd /d "D:\postgresql-9.6.0-1-windows-x64-binaries\pgsql\bin"
start "" "pg_ctl" start -D "D:\pg_data\data"
echo server started
pause
change to start "" /wait if you want to wait for the service to start before you echo it has been started.

check existing file in ftp batch file [duplicate]

I am writing a quick batch to see if a file exists after executing an exe.
The file is created with a YYYYmmDDnumbernumbernumber.xml file name according to the current date.
How do I check for the file with a variable in the beginning? Here is what I have so far:
#echo off
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
if not exist "ftp://FTPsite/%mydate%*.xml" (echo nah) else (echo yea)
pause
you cannot check existence of file on ftp server with IF.Instead try this after replacing the parameters that starts with MY_ :
!cls&echo off&setlocal ENABLEDELAYEDEXPANSION
!cls&goto :ftp_end
open MY_FTP_SERVER
user MY_USER
pass MY_PASS
cd MY_REMOTE_DIR
ls . local.file
bye
:ftp_end
ftp -s:%0
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
type local.file | findstr /B "%mydate%" | find ".xml" && echo FILE IS OUT THERE && goto :skip_file_is_not_there
echo FILE IS NOT THERE
:skip_file_is_not_there
del local.file /q >nul

Works properly, but not when running as administrator

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

FTP commands in a batch script does not working properly

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

batch file creation pse

I'm trying to run a batch file that will connect to the list of ip's on my network, open up cmd then run a list of commands: like ipconfig /all, nbstat -c, arp -a. Then it must save the results into a folder renamed as that "computername".
I already have a batch file made that can do the commands I want and create a folder with the computer, then input the different commands into txt files within that folder.
Here is the WindowsCommands batch file:
md %computername%
echo off
echo ARP Command
arp -a >> %cd%\%computername%\arp-a.txt
echo NBSTAT Command
nbtstat -c >> %cd%\%computername%\nbstat.txt
echo Ipconfig Command
ipconfig /all >> %cd%\%computername%\ipconfig-all.txt
echo Ipconfig DNS Command
ipconfig /displaydns >> %cd%\%computername%\ipconfig-displaydns.txt
echo Netstat Command
netstat -ano >> %cd%\%computername%\netstat-ano.txt
echo Tasklist Command
tasklist /v >> %cd%\%computername%\tasklist.txt
echo LG Admin Command
net localgroup administrators >> %cd%\%computername%\netlocalgroupadmin.txt
echo Directory Command
dir C:\Windows\Prefetch >> %cd%\%computername%\prefetch.txt
exit
I also created a hosts.txt file that contains my local Ip addresses that I want to run the commands on.
I also created another batch file name psexec for running a For loop.
Now my troubles and arising when trying to run the psexec batch file.
Here is my psexec file:
for /f %%a in (hosts.txt) do (
psexec \\%%a C:\Users\ISSG\Documents\WindowsCommands.bat
)
Now that is just a rough draft I'm not entirely sure if that is how it should be coded. This is one of the first automated scripts I have ever wrote.
So in a nutshell i need to be able to run this batch file from my local computer- psexec into the IP's. Gather the information and output it into txt files on my local computer.
If anyone could point me in the right direction that would be great!
Thanks!
If your Batch file doesn't exist in the system directory on all of the computers, you have to use the -c switch to copy it to them in order to run it. It's a good idea to try to ping the computer first to save time trying to connect to it.
for /f %%a in (hosts.txt) do (
for /f "tokens=2 delims=:" %%b in ('ping -n 1 %%a ^| find "TTL="') do (
if errorlevel 0 (
psexec \\%%a -c -f -u username -p password C:\Users\ISSG\Documents\WindowsCommands.bat
)
)
)
Also, keep in mind that this will create the folder in the System32 directory on the remote computer. If you want it on your local drive, do something like this:
#echo off
FOR /F "tokens=2" %%A IN (
'net use * "\\computer\share"'
) DO IF NOT %%A.==command. SET dl=%%A
if not exist "%dl%\%computername%" md "%dl%\%computername%"
echo ARP Command
arp -a >> %dl%\%computername%\arp-a.txt
echo NBSTAT Command
nbtstat -c >> %dl%\%computername%\nbstat.txt
echo Ipconfig Command
ipconfig /all >> %dl%\%computername%\ipconfig-all.txt
echo Ipconfig DNS Command
ipconfig /displaydns >> %dl%\%computername%\ipconfig-displaydns.txt
echo Netstat Command
netstat -ano >> %dl%\%computername%\netstat-ano.txt
echo Tasklist Command
tasklist /v >> %dl%\%computername%\tasklist.txt
echo LG Admin Command
net localgroup administrators >> %dl%\%computername%\netlocalgroupadmin.txt
echo Directory Command
dir C:\Windows\Prefetch >> %dl%\%computername%\prefetch.txt
Net use %dl% /delete

Resources