not able to delete file from command line in windows server 2008 - batch-file

I am writing a batch program in windows server 2008
I am using robocopy command and /move parameter to copy and delete the file from source path. Not able to succeed.
Then I tried using DEL dos command. Not successful.
Throwing access error
Could you please let me know the dos command to forcefully delete a file or clear the contents of the file.

assuming the file is write-protected, try:
del /f file

Sounds like you don't have permission to that file/folder. Even if you are an admin... open an administrative Command Prompt by Right Clicking on CMD.exe or shortcut and 'Run as administrator'.
AN administrator does not have the same privileges as THE local Administrator.

Related

Cannot run a batch file from SQL Server Agent (Access denied)

I am trying to execute a batch file from SQL Server Agent (as it needs to be done before some SSIS-packages are run).
When I execute the job it fails in a few seconds saying "Access denied".
The account under which SQL Server Agent runs has full control on the folder that contains the batch file. The result of the batch would be deleting some files in a folder, calling a webservice and get those same files back from the webservice.
I can run the batch file when I start it with my own (admin) account.
I googled and found several other questions and answers but none of those were covering my problem. I hope you can point to other options.
Thanks for your help.
Johan
Batch file contents:
echo Removing txt files of last run
del Employees.txt
del HrDepFun.txt
del HrEmpActual.txt
echo Files removed
echo Starting getconnectors
{Call Webservice} -> cannot disclose this on stackoverflow
echo Getconnectors done
Batch file execution statement from SQL Server Agent job (type Operating System (CmdExec)):
cmd.exe /c "c:\Program Files (x86)\AFAS\AFASRemote_Call_GetConnectors.bat" > connectorlog.txt 2> connectorerrorlog.txt

How can I end a process with admin privileges using a batch file

I have written a batch file which starts OpenVPN. If the client is found, I check the connectivity by running PING. If the server is not found, then this means that although OpenVPN is running, the connection has not been established. At this point I want to restart OpenVPN. Because I am not using the admin account for this script, I always get the error "permission denied" when using TASKKILL /F. Is there anyway around this?
There are two methods of running a command with administrator privileges.
Solution 1:
Right-Click the batch-file and click run as admin.
Solution 2:
Use the runas command to run a command or batch file with administrative permissions. This way the process is still automate but you will still need to type in the password for administrator.
Example:
C:\> runas /user:administrator "taskkill /f /im explorer.exe"
Enter the password for administrator:
Type runas /? for more info.
if you are already the administrator, you can create a shortcut to your batch file and when you right click the shortcut -> properties -> advanced -> check the checkbox "run as administrator"
and voila!
everytime you run the batch script via this shortcut file, it will run as administrator and will kill without any discrimination, happily ever after

Need help writing .bat file which changes all file permissions in a folder to Everyone

I appreciate your time so I won't take much of it. Here's what I'm trying to do: I want to create a .bat file that, when executed, will open the Command Prompt program, change directory to a folder (located on the C:), then execute a line of code that will change the file permission status of all files in that folder to Everyone. I intend to create a Windows task that will run this .bat file everyday. I understand how to setup the Windows task, but I can't figure out exactly how the .bat file should be written. Can anyone help?
Here's what I've got so far:
ECHO OFF
[Tab]Start "" C:\Windows\system32\cmd.exe
ECHO OFF
[Tab]Prompt cd cd:\google drive
ECHO OFF
[Tab]Prompt cacls *.* /t /e /g everyone:f
If you're interested, here's why I'm trying to create this .bat file:
I use a single Google Drive account on two file server computers, one in each of two offices. Each office has this file server and about 10 client computers. The client computers access files on the shared Google Drive folder (located on the file server) and occasionally add/edit/delete files. Google Drive does a great job of keeping all files synced between the two offices, but one problem I have is that if office A adds a file, the only computer in office B that can see it is the file server. I have to change the file permission to "Everyone" using the file server in office B before any of the client computers in office B can see the file. Over time, it's become very annoying to manually change the file permission every day and I'm looking for a shortcut. Please let me know if you can think of a better one.
If you got the commands right then this is a batch script with them.
The && will run the following command only if the cd command was successful.
#echo off
cd /d "c:\google drive" && cacls *.* /t /e /g everyone:f

msiexec /a not working

I am trying to create a batch file to run an unattended installation of Adobe Reader 9 (it is a MSI file)
My problem is with the UAC. If I make a command like this:
msiexec.exe /i "AcroRead.msi" EULA_ACCEPT=1 ALLUSERS=1 /passive
The installation runs fine, except that I have to accept the UAC before it does anything. If I replace the /i with /a, it skips the UAC, and it seems to be installing, but when it finishes, it didn't install the program at all! No error message or nothing, it does not show up on add/remove programs.
Might it be that it installs to the wrong profile if I use the /a switch?
I am very new to scripting, so most of the info I have is what I can scavenge from google.
I know there are ways to disable the UAC from the batch, but if possible I would like to avoid that.
I am doing this on a virtual Win7 x64 sp1 machine.
Any software installation should prompt for admin credentials regardless of whether you launch the installer via the batch script or double-clicking the AcroRead.msi file in Windows Explorer. This is how permission on Windows works, and to be honest, I wouldn't want a system that could be bypassed via a terminal WITHOUT prompting for credentials. I don't believe there's any way around this.
Try removing /passive, since maybe that is suppressing an error message. Also insert a pause statement at the end of your batch script so you can read the error message before the command prompt is closed.
Edit:
Run the batch script as an administrator. This is different than running msiexec with the /a command. This will open the UAC prompt at the beginning of the script, and if authenticated, the rest of the batch file will be run with admin privileges.
/a is not used for a normal installation. It will generate an administrative installation. A "network" installation to allow other users to install from a common repository.

Batch file when using WinSCP and command prompt

I am currently writing a script that calls WinSCP, connects an SFTP session, transfers a group of files from a local server to a remote server, closes the connection, then moves the local file to an archive. An alternate acceptable solution would be to copy the file to archive and then delete it from the source directory.
The problem I am having is that I can get WinSCP to open, connect and transfer the files, but after that the commands are ignored and I am unable to automate the connection close, file move or copy and the deletion and closing of command prompt. What is the best way to do this?
My FTP.bat file that connects the session and calls the script -
"C:\Program Files\WinSCP\WinSCP.exe" /console /command "lcd d:\bofa_ftp\out" /script=script.txt savedsession#winscpname.com
My script file that puts the file and closes the connection -
option batch abort
option confirm off
option exclude script.txt
put *.txt
close
When I add any commands to either the bat or the script they are ignored.
Commands after close are definitely not ignored. Your problem might be that you are trying to put Windows commands to WinSCP script. Note that there are no commands in WinSCP to move local files.
Instead of trying using Winscp3, try with plink.exe.
Plink is the best option to transfer files from winscp3 to your local.By using scp command we can transfer file from winscp to local as well as one folder in to another folder in winscp.
scp username#atechguideserver1.com:/data/tmp/samplscp.txt username#atechguideserver2.com:/data/tmp/dir/tech
Please refer to: http://99students.com/move-files-in-unix/

Resources