Im in Windows Server 2003 and using below commands to fetch string matching lines in files created today in a specific directory.
forfiles -p D:\ -m *.1 -d +0 -c "cmd /c findstr /i /c:\"Hey Hi\" #FILE" >> txt5.txt
or
forfiles -p D:\ -m *.1 -d +0 -c "cmd /c grep \"Hey Hi\" #FILE" >> txt5.txt
Geeting error 'FINDSTR: Write error' and 'grep write error bad file descriptor' respectively for both commands. So basically the commands work i.e. display the output in screen but unable to redirect the output to a file.
I did not find suitable solution though users reported this same error for different scenarios. Any help is appreciated. Thanks in advance!
You should be able to remove the cmd /c; I don't see any reason you'd need a new copy of the command shell open for the findstr call.
This works for me correctly at the command prompt:
forfiles -m t*.xml -d +0 -c "findstr /i "Item" #file" >> out.txt.
It produces an out.txt file that contains the proper content matching the search criteria.
Related
I'm having trouble deleting the sub-directory, while I'm executing the following batch via cmd. The mentioned dir is getting deleted, and I'm getting the relevant log file of the deleted files, but the sub-dir isn't getting deleted. Since it ain't getting deleted, I don't have a proper log file.
Here is the code that I'm executing,
#echo off
SETLOCAL
:: set folder path
set dump_path=%1
:: set min age of files and folders to delete
set max_days=%2
shift
shift
:: remove files from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c del /q #path && echo #path>>logfile.log"
:: remove subdirectories from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c IF #isdir == TRUE rd /S /Q #path && echo #path>>logfile.log"
Any help is appreciated.
Below is the pic, in that further, I added a folder with some contents to check, but it ain't working.
More info, as it still ain't working, any way to change it into a PowerShell execution?
Below pic, before executing the cmd,
Post execution of the cmd,
Still, I'm unable to understand why it isn't removing the sub-dir files...This time, I removed the sub-dir cmd from my batch and added a '/S' to my first cmd.
There are two problems with your batchfile:
You mention you want to remove the files with are older than 15 days, but in your batchfile you remove the files which are more recent than 15 days (there's a minus in front of the %max_days%, not a plus, you need to change that).
You mention launching the batchfile, using 15>nul as a second parameter, which is wrong. It should simply be 15.
The following question has helped me solving the problem of executing multiple SQL Scripts located in file. Run all SQL files in a directory
However, I did not get how to redirect the output into a separate log file. Someone suggested the following script but since I don't understand it, it did not work and I can't find out the error.
for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1)
If you need the output into one common file then you should use the #Abhishek 's answer.
If you need the output into a separate log file for an each input sql file
then you can use -o parameter of sqlcmd command. Your bat file could look like this:
for %%G in (*.sql) do sqlcmd /S <servername> /d <dbname> -E -i"%%G" -o C:\logs\%%G.log
pause
In this case for
1.sql
2.sql
you will get:
1.sql.log
2.sql.log
You are seeking Command Redirection.
As per your example -
for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1
once the execution of the sql script is done the output will be redirected to and appends the command output to the end of file (here sql.log) without deleting the information that is already in the file (>>) and redirects STDERR (2) into STDOUT handle(1) - 2>&1
More information here and here.
Here is the script that I am currently working with:
forfiles -p E:\datatel\coll18\developmentSQL\apphome\_HOLD_\ -s -m * /D -28 /C "cmd /c del #path"
The -s should allow me to delete files within the sub-directories without removing the folders, but instead I receive a "No files found" error. Am I overlooking something simple? I've tried switching the - to a / for continuity, but the results are the same.
I wanted to remove the last forward slash of a directory string so that I use the same path for forfiles tool to delete old files. I have tried the solution here but it didn't work. The script saves the output to a text file so that I see the result. The forfiles doesn't work as the directory is incorrect format.
What I want to just to remove the last backward slash "C:\Database\Backup\".
The runable code below is what I have attempted. The problem is line 6, which outputs "C:\Database\Backup\" :~0,-1 instead of "C:\Database\Backup"
set BACKUP_DIR="C:/Database/Backup/"
set LOG_FILE=%BACKUP_DIR%log_file.txt
if not exist BACKUP_DIR mkdir %BACKUP_DIR%
set BACKWARD_SLASH_DIR=%BACKUP_DIR:/=\%
echo %BACKWARD_SLASH_DIR% > %LOG_FILE%
set DELETE_DIR=%BACKWARD_SLASH_DIR%:~0,-1%
echo %DELETE_DIR% >> %LOG_FILE%
forfiles -p %DELETE_DIR% -s -m *.* -d -1 -c "cmd /c echo Deleted #file" >> %LOG_FILE%
forfiles -p %DELETE_DIR% -s -m *.* -d -1 -c "cmd /c del #path"
This is a good example of why you should always set "variable=value" with the "variable=value" pair quoted. That way, the quotation marks aren't part of the variable value, and you can explicitly quote "%variable%" when necessary. It also removes ambiguity and ensures that there's no trailing whitespace inadvertently added to the end of the value. Matter of fact, in the code you pasted, there is an extra space at the end of your set BACKWARD_SLASH_DIR=%BACKUP_DIR:/=\% line.
Basically what I'm trying to do is create a batch file and place it in my startup that will use the modified date of the same batch file and see if it is greater or less than 90 days old. If it isn't, nothing happens and life goes on, but if it is, I want it to delete the contents of my downloads folder. I have a little bit of coding, but I've been testing it, but for some reason it isn't wanting to work for me. Any help would be greatly appreciated.
forfiles -p "C:\LOCATION OF .BAT\" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
I'm just starting out using batch commands and would really love the help
This works for you if you are using Win2003 or WinXP and have forfiles.exe installed on your machine...
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat -d -90 -c "cmd /c del C:\user folder\Downloads"
Later versions of Windows and Windows Server have it installed by default.
For Win7 or higher: Syntax has changed a little therefore the updated command is:
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
Assuming your target path contains spaces etc, then use this in your forfiles command. The 0x22 represents a double quote character.
If it echos the command then try it without the echo to actually perform the deletion
"cmd /c echo del 0x22C:\user folder\Downloads\*.*?0x22"