

InterviewSolution
Saved Bookmarks
1. |
Solve : Batch help: search for file in profiles, then delete? |
Answer» <html><body><p>Hey there team...Newb to the forum...from my searching around it looks like a <a href="https://interviewquestions.tuteehub.com/tag/quite-1175131" style="font-weight:bold;" target="_blank" title="Click to know more about QUITE">QUITE</a> knowledgeable group...i've got 10+ yrs in the IT/IS field, but have never really had to script anything until now (except for back in college...i know i know...don't use it you lose it)<br/><br/>all PCs are XP Pro<br/><br/>i'm trying to get a batch going that will search for a *.mht file in every profile on the PC and delete it. when i build the PCs for everyone, they have a MHT file that all the agents use and so i always save it to the all users desktop folder. after a long run, they have recently updated the file and i need to replace it...so it needs to be deleted from the all users and actual user's profiles on the PCs...<br/><br/>i start off by having the batch create a txt file that has all the *.mht file locations and then i use the "for /f" command to delete the contents of the txt file, but it can't get anywhere because the file format of the txt file is C:\Documents and Settings\...and i can't get the txt file to create the path with " " around the path. the reason i want it gone from only the DocSettings folder is that some of the agents have requested to still have a copy of the old file...i've instructed them to save it to another folder on their hd...so i don't want to have a blanket delete<br/><br/>is there something that i'm missing? a simpler way to accomplish this? i used the same method when i created a batch for deleting all the $*unin*$ files w/no probs but it was never forced to deal with paths with spaces...this seems simple enough but for some reason i'm pulling a blank...<br/><br/> Code: <a>[Select]</a>echo off<br/>C:<br/>cd "\Documents and Settings"<br/>dir /b /s *.mht > oldAppGate.txt<br/>for /F %%i in (oldAppGate.txt) do <a href="https://interviewquestions.tuteehub.com/tag/del-947443" style="font-weight:bold;" target="_blank" title="Click to know more about DEL">DEL</a> /<a href="https://interviewquestions.tuteehub.com/tag/q-236607" style="font-weight:bold;" target="_blank" title="Click to know more about Q">Q</a> %%i<br/><br/>this is the content of "oldAppGate.txt":<br/>C:\Documents and Settings\Agent1\Desktop\Application Gateway1 6-20-2010.mht<br/>C:\Documents and Settings\All Users\Desktop\Application Gateway1 6-20-2010.mht<br/><br/>the command window states:<br/>Could Not Find C:\Documents<br/>Could Not Find C:\Documents<br/><br/>(and yes i'm deleting the updated file...its on my test PC )<br/><br/>thanks!<br/>Hi<br/>You got it right in the CD line, paths with spaces in need quotes, try this<br/> Code: <a>[Select]</a>echo off<br/>C:<br/>cd "\Documents and Settings"<br/>dir /b /s *.mht > oldAppGate.txt<br/>for /F %%i in (oldAppGate.txt) do del /q "%%i"<br/>Note the %%i is between quotes on the last linegpl...gotcha...will try that and report back.<br/><br/>thanks!<br/>lvgpl...nope! doesn't work...still the same result as initially reported in my first post<br/><br/>any other thoughts?<br/>lvcould you post the code as you have it now ? It definitely should not give the same error -- the message<br/>Could Not Find C:\Documents<br/>is a dead giveaway, as it is stopping at the first spaceyes, because the txt file's info doesn't have " " around the path...so when it reads the file, it will stop at C:\Documents which is what i thought you were trying to do with the " " around %%i...but it seems to not work...?<br/><br/>the only thing i changed was your suggestion to add " " around %%i...see below:<br/><br/> Code: <a>[Select]</a>echo off<br/>C:<br/>cd "\Documents and Settings"<br/>dir /b /s *.mht > oldAppGate.txt<br/>for /F %%i in (oldAppGate.txt) do del /q "%%i"<br/><br/>pause<br/>What might be useful is to remove the echo off line<br/>then each command line will be echoed so that you can see it - that might <a href="https://interviewquestions.tuteehub.com/tag/help-239643" style="font-weight:bold;" target="_blank" title="Click to know more about HELP">HELP</a> narrow down the problemif any line in oldappgate.txt has a space or spaces then for /f will stop at the first one (since it is one of the default token delimiters) so you need to force the delimiters to be the start and end of the line with a "delims=" block. So try this...<br/><br/> Code: <a>[Select]</a>for /F "delims=" %%i in (oldAppGate.txt) do del /q "%%i"<br/><br/><br/>doh<br/>typical schoolboy error, sorry I missed that one! Quote from: Salmon Trout on June 23, 2010, 08:59:13 AM</p><blockquote>if any line in oldappgate.txt has a space or spaces then for /f will stop at the first one (since it is one of the default token delimiters) so you need to force the delimiters to be the start and end of the line with a "delims=" block. So try this...<br/><br/> Code: <a>[Select]</a>for /F "delims=" %%i in (oldAppGate.txt) do del /q "%%i"<br/><br/><br/><br/></blockquote> awesome...that did the trick! thanks a ton! Quote from: gpl on June 23, 2010, 09:09:33 AM<blockquote>doh<br/>typical schoolboy error, sorry I missed that one!<br/></blockquote> no biggie! i haven't used For /F that much so i had been scratching my head all day on that one...i felt like i was going crazy...thanks for the help just the same!so i put this script out on a network drive and i had one of the users run it...but it gives an error that it doesn't like being run from a network drive...i don't get it...i tell it to start in C:\...what i'm eventually going to do is have this batch file run when someone logs in so that its done for everyone...is there something i need to call out so that it will run over the network? or is this going to pose a problem for me?ok...so i ran this from my PC and it worked...yet the only difference is that i have it saved in a network drive that is actually <a href="https://interviewquestions.tuteehub.com/tag/mapped-2815499" style="font-weight:bold;" target="_blank" title="Click to know more about MAPPED">MAPPED</a> on my PC (but not mapped on the user's PCs, yet it is a shared folder on the server)<br/><br/>in order for this to work, does this batch file have to be in a mapped network drive instead of a shared folder on the network?<br/><br/>if i just copy/paste this directly into their logon script will i have the same problems and will their lack of admin/pwer user rights cause any problems for my script?</body></html> | |