| 1. |
Solve : batch for searching string? |
|
Answer» I have maded batch which search strings and if it finds it in txt file then it delete some other txt file. Did you try using the /S option? thanks, but things are more complicated if I use S option under e:\uporabniki it will delete all date.txt under e:\uporabniki but that must not happend. In bkpXXXX.txt is log what is backuped and if there is a string in it "disk is full" than it is full just for one user not for all, because of quotas. So file date.txt must be deleted only under that user which have in backup log "disk is full" because if that file mising from users log I get message that somethig is wrong with that user. for EXAMPLE e:\uporabniki\mike\documents\logs e:\uporabniki\john\documents\logs e:\uporabniki\george\documents\logs e:\uporabniki\anna\documents\logs script recognise in e:\uporabniki\anna\documents\logs is file e:\uporabniki\anna\documents\logs\bkpXXXX.txt is string "disk is full" and script msut than delete only this file e:\uporabniki\anna\documents\logs\XXdateXX.txt all other must leave alone e:\uporabniki\mike\documents\logs\XXdateXX.txt e:\uporabniki\john\documents\logs\XXdateXX.txt e:\uporabniki\george\documents\logs\XXdateXX.txtdel /s /q e:\uporabniki\%user%\*date*.txt Set user to the username of the one it finds has a full disk.this way script must be runned by user, I intent to do it on server with admin. I will CHECK this how it worksI am confused. You know what user you need to delete it from, because that is what the log file is telling you. PULL the name out of the log file. Quote from: Squashman on April 20, 2015, 12:15:06 PM I am confused. You know what user you need to delete it from, because that is what the log file is telling you. Pull the name out of the log file. Ok that is possible, I have a txt file with name of user in log folder like this John114S-HPTT.txt So how can I pull that name and use it in my script?Isn't the log file you want to delete at e:\uporabniki\\documents\logs? Just do this to the path: Code: [Select]for /f "tokens=3 delims=\" %%A in (<path>) do set user=%%A Quote from: Lemonilla on April 20, 2015, 04:30:24 PM Isn't the log file you want to delete at e:\uporabniki\<SomeUser>\documents\logs? yes but just one log of many and that is only this one *date*.txtCan you post an exact tree of the files and which ones you are searching through and which ones need deleting? EDIT: If you could also post the actual file you are looping through, we might be able to help you more.here are two users from 50. script search it in file bkpA_desktop.txt bkpA_documents.txt bkpA_cpi_documents.txt bkpA_cpi_desktop.txt bkpAHP_desktop.txt bkpAHP_documents.txt If script FOUND text disk is full "Na disku ni dovolj prostora." Than it must delete all files *datet.txt for example these files 14.04.2015--datet.txt 13.04.2015--datet.txt 10.04.2015--datet.txt 09.04.2015--datet.txt 08.04.2015--datet.txt and that is for all 50 users other log files must stay as they are in folder log. [attachment deleted by admin to conserve space]this can't be done? is it impossible? Quote from: Blisk on May 04, 2015, 12:35:06 AM this can't be done? is it impossible? Your question is impossible for me to understand. Quote from: Blisk on April 20, 2015, 02:42:42 AM which search strings and if it finds it in txt file then it delete some other txt file. X X X and X are not really showing the relationship of the match. I think the task is really really hard to understand from what you have written and shown. Actual real life examples are useful, along with the description of the task.I did that two posts above and the tree of folders is also thereCouldn't you break this into steps and solve that way? Step 1: Collect full users Code: [Select]for /f "delims=" %%A in ('dir /b e:\uporabniki') do ( findstr /m "Na disku ni dovolj prostora" e:\uporabniki\%%A\documents\logs\bkp*.txt if not %errorlevel%==1 ( echo %%A >> e:\temp\userlog.txt ) ) Step 2: Delete files Code: [Select]for /f "delims=" %%D in (e:\temp\userlog.txt) do (del /q e:\uporabniki\%%D\documents\logs\*date*.txt) Or combine them into one script and call it a day. Code: [Select]echo off for /f "delims=" %%A in ('dir /b e:\uporabniki') do ( findstr /m "Na disku ni dovolj prostora" e:\uporabniki\%%A\documents\logs\bkp*.txt if not %errorlevel%==1 ( echo %%A >> e:\temp\userlog.txt ) ) for /f "delims=" %%D in (e:\temp\userlog.txt) do (del /q e:\uporabniki\%%D\documents\logs\*date*.txt) del /q e:\temp\userlog.txt EDIT: I just thought on this, it would cause issues as the errorlevel would continue to rise with each loop, therefore not giving a proper reading. You would need to set the errorlevel after each run, but we don't want to do that because errorlevels don't work that way. So we have to try something different. Code: [Select]echo off setlocal enabledelayedexpansion set errorlev=%errorlevel% for /f "delims=" %%A in ('dir /b e:\uporabniki') do ( findstr /m "Na disku ni dovolj prostora" e:\uporabniki\%%A\documents\logs\bkp*.txt if !errorlevel!==!errorlev! ( echo %%A >> e:\temp\userlog.txt ) set errorlev=!errorlevel! ) for /f "delims=" %%D in (e:\temp\userlog.txt) do (del /q e:\uporabniki\%%D\documents\logs\*date*.txt) del /q e:\temp\userlog.txt Give that a shot. I would change the del command to an echo del command for your first run to make sure you are deleting what you want to delete. Then you can remove the echo and run the script. |
|