Multiple commands in batch file - batch-file

I have a batch file that I have scheduled to run in Task Scheduler, but it doesn't seem to run the second command "forfiles" when the Task runs. Here is my batch file:
sqlcmd -S WEB01\SQLEXPRESS -U username -P password -i "D:\BackupPrograms\cslogsbackup.sql"
forfiles /P "Z:\Logs" /S /M *.bak /D -7 /C "cmd /c del #file"
Is there something that needs to go between the sqlcmd and the forfiles lines?
If I click on the batch file and run it manually it works fine, just does not delete the older files in the "forfiles" line when the batch files runs in the Task Scheduler.

Related

windows batch file not exiting

I am not good at windows scripting. (I am linux guy)
I have to delete files which they are older than n days in two directories.
I am using 2 forfiles command in script. When I run the script manually there is no problem so it's running and finishing its job properly and it takes about 30 seconds to finish
So I created a task to run this batch file everyday in task scheduler
When Task Scheduler executes the script, the script could not end and I could see a running forfiles command in task manager and following day task manager creates a log the script/command is already running. So I am terminating script and run "manually" again, it successfully run and finishes its job in 30-35 seconds again
My script is like that
y:
cd \<path1>
forfiles /s /d -120 /m *.* /c "cmd /c del #path"
cd \<path2>
forfiles /s /d -120 /m *.* /c "cmd /c del #path"
EXIT
How can I run this batch file from task manager properly ?
Thanks
To me it sounds like rights issue. How do you execute your script (directly via start a program or via batch file)? Which windows you are running?
On Windows 7 and Task Scheduler:
Have correct user filled out - who has the rights to run the task and access the files.
Try running your script with checkbox Run with highest privileges checked.
Edit
Stefan is right. If you are running it from within batch file you have to map the network drive with correct credentials:
net use <driver letter> "\\<servername>\<sharename>" /user:<username> <password>
You could do it the following way:
IF NOT EXIST y:\ (
ECHO "The y: drive does not exist. Mapping"
net use y: "\\<servername>\<sharename>" /user:<username>
)

Access is denied deleting files at command prompt

I am trying to run a batch file nightly to delete files in a certain directory that are older than 15 days. I am getting "Access is denied" even if I paste the command into an elevated (admin) command prompt window. This is on a Windows 7 PC. Here is the command that I found online to delete files over a certain age:
forfiles /p "C:\ProgramData\WEBSERVER\Download\X549D95" /d -15 /c "cmd /c del #path"
The user I am logged in as is a local administrator and has full control to the files in that folder. The PC is not a member of the domain so should not have group policies doing anything weird on it.
Maybe I have to do something to make this part of the command run as an administrator: "cmd /c del #path"? If so, how do I do that? I cannot find a switch for "cmd" to do that.

ForFiles return code 1 within Windows Task Scheduler

I am trying to delete files from a 2008 Windows Server that are older than 180 days based on the last modified date. I am using ForFiles in order to attempt to do this. When I run the command interactively as domain admin on the server, the command executes. When I setup a task within Windows task scheduler, the task fails to run with a return code of 1.
The below command is the command I am running.
forfiles /p "E:" /s /m *.* /D -180 /c "cmd /c del #path && echo"
I have also tried the below:

Delete files over network

I have two scripts run by the task scheduler.
First batch file makes a file on network shared folder for each computer in network
and it works ok
%SystemRoot%\system32\cmd.exe /c "copy /y nul \\mysrv\test\%computername%\pcon.txt"
Second script must delete that file, but doesn't delete it, how to solve that.
It runs only if I double click on it
Task is run by user SYSTEM in task scheduler
%SystemRoot%\system32\cmd.exe /c "del /Q \\mysrv\test\%computername%\pcon.txt"

forfiles not working in scheduled task

I have created a batch file with the following line:
forfiles /p L:\percepsrvr\SQL /s /m *.bak /d -4 /c "cmd /c del /q #PATH "
This deletes any files older than 3 days and works when I run the batch file.
However, when I try to run this batch file as part of a scheduled task the line of code fails to execute. I think it might be because of the mapped network drive (L:) but don't know for sure.
I have the scheduled task running as myself to make sure it has the same permissions as when I run it manually. The scheduled task is running on a Win2008r2 server box and L:\ is on a Win2003 SP2 server box.
Anyone know what might be keeping this from working properly or how to debug a scheduled task?
TIA
Brian
In case anyone comes across this post I found a solution as follows:
ROBOCOPY "D:\Backup Data\percepsrvr\SQL" "\\belgrade\v$\percepsrvr\SQL"
ROBOCOPY "\\belgrade\v$\percepsrvr\SQL" "\\belgrade\v$\percepsrvr\SQL\Old" *.* /move /minage:10
DEL "\\belgrade\v$\percepsrvr\SQL\Old\*.*" /Q
Basically, move the files to delete to another directory and then delete that directory (which can be done with RoboCopy).

Resources