There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
DOS/PDA/Other
Tag Cloud
audio bios blue screen boot bsod computer connection crash dell desktop email error excel firefox freeze freezing google hard drive hardware hijackthis install internet laptop linux malware network no sound outlook problem recovery redirect router screen slow sound speakers spyware startup trojan usb video virus vista vundo webcam windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/PDA/Other >
Solved: Batch File - For Loop Counting Characters within XCopy

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
scrfix's Avatar
Computer Specs
Senior Member with 249 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
04-Jun-2009, 06:43 PM #31
You do not have a pause in there to be able to tell if there are any errors. Since the window automatically closes after it finishes and the window closes upon crashing, it will seem like there are no errors.

Compile the below and run the exe. The pause will never happen.

Code:
rmdir /S /Q %temp%
pause
Now, the weird thing is that when I only use the del function with what I did above, it actually works and gets to the pause but if I have any commands beyond that it crashes. Still the same concept as explained above.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets, Corporate Gifts
Resources LLC: Water Treatment Coagulation, Water Recycling
Squashman's Avatar
Distinguished Member with 14,920 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: IIAHYAYCESA,YAADA!
04-Jun-2009, 08:27 PM #32
Posted via Mobile Device
If it deleted everything in the folder except for files in use what errors do you think I am going to get. you have to remember that the vista cmd prompt does not run with full administrative privileges by default.

I don't need to put a pause into the batch to know whether it worked or not. If it clears out all the folders and files it worked which it has done for me every time. Any remaining files and folders it didn't delete I know are in use with another app I have on my pc.
scrfix's Avatar
Computer Specs
Senior Member with 249 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
04-Jun-2009, 08:30 PM #33
If it doesn't get to the pause it doesn't work. It won't continue onto the rest of the batch file.

I mentioned above the errors you will get.

I am confused. You asked me to be more clear about what I am asking yet you are not willing to go forward with it when I am?
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets, Corporate Gifts
Resources LLC: Water Treatment Coagulation, Water Recycling
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 5,315 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
05-Jun-2009, 02:22 AM #34
When you execute the compiled EXE file, it creates a randomly named folder in the Users Temp folder (%temp%), then extracts the batch file it contains into that folder. Any other files you included will be extracted to the Working Directory, which will be this temp folder if you chose Temporary Directory for the Working Directory. The folder is named HHHH.tmp, where H is a hexadecimal number. (Just a FYI, there is no predefined variable for the System temp folder C:\Windows\Temp).

If you delete everything in the %temp% folder, or remove the folder, the program is going to crash, that's just the way it works.

You can use Del to delete all files in %temp% then a For loop to remove all folders except the one containing the extracted file(s). A bit convoluted to exclude the root %temp% folder and the one created, but doable:
Code:
Del /F /Q *.*
For /R "%temp%" %%I In (.) Do If /I Not "%temp%\."=="%%I" If /I Not "%%~sI\"=="%~dps0" RD /Q /S "%%I"
You could also do it this way but there's no guarantee that the names compared in the If statement will consistently be short or long.
Code:
PushD %~dp0
Set _batpath=%CD%
cd ..
Del /F /Q *.*
For /F "Delims=" %%I In ('dir /AD /B') Do If /I NOT "%_batpath%"=="%%~fI" RD /Q /S "%%I"
PopD
Why do you need to clear out the folder before running your program?
If it's to avoid copying the contents as part of a backup, it's best to just exclude the folder using the xcopy/robcopy switch. If not excluded, your batch file and any other files wrapped up with it will be copied, which you may not want to have happen.

If it's to remove temp files you've created, just use a subfolder for your temp files and then delete the subfolder.

If you want to clean out the %temp% folder as part of a system cleaning application, you'd need to create a batch file in another folder (like the System temp folder) and execute it as the last step in your batch file. It would then clear the User temp folder on exit, and then delete itself.

So the last three lines before the file ends would be:
Code:
>%systemroot%\temp\cleantmp.cmd Echo RD /Q /S "%temp%"
>>%systemroot%\temp\cleantmp.cmd Echo Del %systemroot%\temp\cleantmp.cmd
%systemroot%\temp\cleantmp.cmd
HTH

Jerry
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers ; I just don't always match the answers to the right questions
Are you aware of the New Signature Limitations?
scrfix's Avatar
Computer Specs
Senior Member with 249 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
05-Jun-2009, 09:29 AM #35
Hi Jerry,

Once again, thank you for being precise. I don't know if you read all of my posts however that is what I came up with too. I explained that is what was happening two or three times above. I also mentioned in one of them that I could just exclude and then copy and then at the end of it then delete and who cares at that point if it deleted it.

In one compiler, there is an option to change working directories however it doesn't work. I have wrote the maker but who knows if they will get back to me. They make it sound like months before they get back to you if they get back to you.

I would rather those compilers be a true compile and turn the code into a DLL or something written in C#, etc etc. I guess we live with the limitations we are provided.

I am still checking into the Robocopy switches and other items so I have not switched it from xcopy to robocopy yet. I wanted to take care of this issue first because it didn't matter what command I was going to utilize, it was still going to crash.

I did attempt to utilize /EXCLUDE:%temp% and it crashed.
Is there a better way?

Code:
PushD %~dp0
Set _batpath=%CD%
cd ..
Del /F /Q *.*
For /F "Delims=" %%I In ('dir /AD /B') Do If /I NOT "%_batpath%"=="%%~fI" RD /Q /S "%%I"
PopD
So if I understand this correctly.
PushD %~dp0 (Change the directory to the current working directory where the batch is being run from.])
Set _batpath=%CD% (Set the variable _batpath equal to the current directory)
cd .. (Go up one level)
Del /F /Q *.* (Silently delete all of the files in that directory however do not touch sub directory files)
For /F "Delims=" %%I In ('dir /AD /B') Do If /I NOT "%_batpath%"=="%%~fI" RD /Q /S "%%I" (Run a For Loop, Look for the delimiters ?? in the directory only listings with names only and store each one in the variable %%I then verify that the directory is not equal to the _batpath variable case insensitive [not sure what the ~f in %%~fI does] and if it is not equal to that, then run the command to remove the directory silently.)
popD (This changes directory again as I understand it however without a directory following it when I enter it into a command line all it returns is the same directory)

Do I have that correct?

This will work only if
1. The maker does not change where they store the bat file. If they decide to store it in the program files directory, this would be disasterous.

2. If we are compiling utilizing that compiler. Other comilers store the bat directly in the temp directory without a folder and that would be disasterous as well to delete all of the files and directories before the temp directory.

So we have two issues.
a. Where someone is making the current working directory.
b. Are we in the temp folder before we execute certain commands.

The following should resolve this issue.
Code:
PushD %~dp0
If %CD%==%temp% (
For /F "Delims=" %%I In ('dir /B') Do If /I NOT "*.bat"=="%%~fI" Del /F /S /Q *.* "%%I"
) Else (
Set _batpath=%CD%
cd ..
If %CD%==%temp% (
Del /F /Q *.*
For /F "Delims=" %%I In ('dir /AD /B') Do If /I NOT "%_batpath%"=="%%~fI" RD /Q /S "%%I"
) Else ( rmdir /S /Q %temp% )
)
PopD
Unless there is a better way to write that.
It should state:
1. Change to the current working directory
2. Check to see if the current working directory is actually the %temp% folder.
3. If it is the %temp% folder then go ahead and delete all files from the current directory and sub-directories that are not .bat files.
4. If the current working directory is not the %temp% folder then let's at this time presume we are in the .tmp folder and let's set the variable _batpath equal to the current directory.
5. Let's go up one level so that we should not be in the %temp% folder however we need to double check this.
6. Let's verify once again that we are in the %temp% directory and not in some program files directory or some other directory of the editors choice.
7. If we are in the %temp% directory, go ahead and delete all files and then delete all sub directories that are not equal to the _batpath variable.
8. If we are not in the temp directory then go ahead and remove the %temp% directory and continue on with the rest of the batch because we are in no danger of deleting the actual bat program.

Hopefully that is what I wrote. Please correct me if I am incorrect or if I have formatting errors or even if there is a much more effecient way of writing this.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets, Corporate Gifts
Resources LLC: Water Treatment Coagulation, Water Recycling

Last edited by scrfix : 05-Jun-2009 10:05 AM.
Squashman's Avatar
Distinguished Member with 14,920 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: IIAHYAYCESA,YAADA!
05-Jun-2009, 10:32 AM #36
I can't explain why it is working on my system and not yours. The batch file executes and leaves 1 directory and 4 files in my %TEMP%. Those 4 files are temp files used by our ADC Time Tracking system and the folder is used by Lotus Notes which I always have open. It can't delete those files because they are in use by the application. I can see how it would not work if it is deleting the source batch file but it shouldn't if the file is in use. It doesn't on my system.

I have never cleared out my TEMP folder ever. I literally had hundreds of files and directories in there before I executed the compiled batch file.
__________________
I hate asking the same question twice!
How to ask questions the smart way!
Microsoft MVP - User Desktop Experience
Squashman's Avatar
Distinguished Member with 14,920 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: IIAHYAYCESA,YAADA!
05-Jun-2009, 10:46 AM #37
Another option. Put the rmdir /s/q in another batch file. Then compile your original batch file but use the includes to add in the batch file with just the rmdir. In the orginal batch file use the start cmd to start the 2nd batch file. to remove the temp directory. The original batch file should terminate after it starts the 2nd batch as long as that is the last cmd in the 1st batch file.
__________________
I hate asking the same question twice!
How to ask questions the smart way!
Microsoft MVP - User Desktop Experience
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 5,315 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
05-Jun-2009, 06:14 PM #38
Quote:
Originally Posted by Squashman View Post
I can't explain why it is working on my system and not yours.
I have to admit, I ran it more than once on both XP and Vista before it sank in Not saying how many times, just more than once.

Maybe this will make it easier to see
Compile the following, run the exe, and note the start and end times:
Code:
@Echo Off
Echo Start time is %time%
Ping 127.0.0.1 -n 1
:: RD /S /Q %temp%
Echo End time is %time%
Pause
Now compile the following, run the exe, and note the start and end times:
Code:
@Echo Off
Echo Start time is %time%
Ping 127.0.0.1 -n 1
RD /S /Q %temp%
Echo End time is %time%
Pause
Quote:
Originally Posted by Squashman View Post
I can see how it would not work if it is deleting the source batch file but it shouldn't if the file is in use. It doesn't on my system.
The Command Processor doesn't lock a running batch file, so it is possible for a batch file to delete itself.
The compiled exe can't be deleted while it's running, but the extracted batch file can be deleted.
Give this one a try:
Code:
@Echo Off
Echo Hi, my name is %0
Echo Try to delete the compiled exe file. I'll wait
Pause
Echo OK, I will now delete myself as soon as you
Pause
Del /F /Q "%0"
:: Comment line to be cached
Echo I've deleted myself so you'll never see this line, and I won't pause
Pause
The line immediately after the Del command may be executed, as it might be cached; without the comment line I can sometimes see the last echo, but the pause is never executed.
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers ; I just don't always match the answers to the right questions
Are you aware of the New Signature Limitations?
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 5,315 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
05-Jun-2009, 06:16 PM #39
Quote:
Originally Posted by scrfix View Post
1. The maker does not change where they store the bat file. If they decide to store it in the program files directory, this would be disasterous.

2. If we are compiling utilizing that compiler. Other comilers store the bat directly in the temp directory without a folder and that would be disasterous as well to delete all of the files and directories before the temp directory.
I tested using this bat to exe compiler, version 1.5.0.0:
Quote:
Originally Posted by Squashman View Post
Once the file is compiled, #1 and #2 won't be an issue. If you recompile using a different version, or a different complier, then you'd definitely need to re-test for both those situations. Good idea to check though. A year from now you may make a minor addition and forget that you're using a different version/compiler.

The first version using the For /R %temp% would be a better choice in this case. That For loop will delete all subfolders in the %temp% directory except for the one containing the batch file, if it exists. Doesn't matter if the batch file is in the temp tree, or someplace else, and it forces the comparison of the paths to use the short names, so no issues if a different compiler/newer version calls the batch file using long names. (more on this below)

You would need to check before running the Del command. I left out the CD %temp% (or PushD %temp%), but the Del command assumes that the batch file and any included files are not in %temp%. If you include other files, and they are extracted into the %temp% folder rather than to a subfolder of %temp%, you'd need to check for and exclude all of them as well.

The 2nd example assumes the batch file is in a subfolder of %temp% and that the subfolder is not a long name, so it would need some modification as well to be more universal.
Quote:
Originally Posted by scrfix View Post
In one compiler, there is an option to change working directories however it doesn't work.
In Kodak's compiler (the one linked above) the Working Directory option does work. It's not explained very well though.
  • Current Directory means the current directory will be set to the directory from which the compiled exe file is run.
    Any included files are copied to the same folder as the exe file when it is run.
  • Temporary Directory will use the Temp folder (HHHH.tmp) that the compiled batch file is extracted into.
    Any included files will be extracted to that Temp folder as well.
    The Submit current directory option will pass the Current Directory as defined above to the batch file on the command line as a quoted string. This will let you use %1 to refer to the folder that contains the compiled exe file.
    You can test with this:
    Code:
    @Echo Current Directory=%CD%
    @Echo Parameters=%1
    Pause
    
There are two ways to refer to a path, using long names or using short names. They are equivalent, but they are not equal.
Example, on XP, my temp folder long name is:
C:\Documents and Settings\TheOutcaste\Local Settings\temp
%temp% expands using the short names to this:
C:\DOCUME~1\THEOUT~1\LOCALS~1\temp
Same path, but an If statement will not see those as equal as they are compared as strings.
On Vista it's
C:\Users\TheOutcaste\AppData\Local\Temp
C:\Users\THEOUT~1\AppData\Local\Temp

If your user name is less than 8 characters long, the long and short names will be the same.

The difficulty arises due to the fact that how %CD% is expanded depends on how you switched to the current directory. This is easiest to see on XP as the long and short names for the normal Temp folder will always be different.
Open a command prompt and type the following (using your username of course) and you'll see the difference:
Code:
CD "C:\Documents and Settings\TheOutcaste\Local Settings\temp"
Echo %CD%
CD %temp%
Echo %CD%
This also assumes that %temp% always expands using short names, which seems to be the case for Win2K-Vista. Haven't tested on Win7 yet. I do have a vague memory of having to use quotes around %temp% because of the space in Documents and Settings, but I may be thinking of one of the other variables. All the rest use the long name format
%0 is the name of the batch file as called. So if called using short names, %dp0 will be the short name version. If called using long name, it will be long. The case should match, but best to use the /I switch just in case.
Short names can be forced using the ~s modifier. See the last section in For /? and Call /? for info on the modifiers.

Quote:
Originally Posted by scrfix View Post
So if I understand this correctly.
PushD %~dp0 (Change the directory to the current working directory where the batch is being run from.])
Set _batpath=%CD% (Set the variable _batpath equal to the current directory)
cd .. (Go up one level)
Del /F /Q *.* (Silently delete all of the files in that directory however do not touch sub directory files)
For /F "Delims=" %%I In ('dir /AD /B') Do If /I NOT "%_batpath%"=="%%~fI" RD /Q /S "%%I" (Run a For Loop, Look for the delimiters ?? in the directory only listings with names only and store each one in the variable %%I then verify that the directory is not equal to the _batpath variable case insensitive [not sure what the ~f in %%~fI does] and if it is not equal to that, then run the command to remove the directory silently.)
popD (This changes directory again as I understand it however without a directory following it when I enter it into a command line all it returns is the same directory)

Do I have that correct?
Pretty much. PushD and PopD work together. PushD saves the current directory on a stack, then does a CD /D to the specified directory. PopD pops the saved directory off the stack, and does a CD /D to the saved directory. This way you don't have to save the current directory in a variable to restore it. PopD will do nothing if there is nothing on the stack. If you are already in the "popped" directory it will seem to do nothing as well.
PushD %CD% doesn't change the current directory, but it does save it onto the stack, which can be useful.
This line should really be PushD %~dps0. This will force it to use the short name version, just in case the compiler calls the file using the long name format. Not likely, but best to eliminate the possibility. This will allow you to compare %CD% to %temp%, as %CD% will use the short name format.
The For loop:
For /F "Delims=" means use no delimiters instead of the default delimiters of tab and space. This means the loop variable will be set to the entire line. Same result as using "tokens=*"
dir /AD /B lists directory names only, not including the path. Does not list files.
"%%~fI" The ~f expands to the fully qualified name. Equivalent to using ~dpnx. The output of this Dir command doesn't include the drive or path, just the name (and extension if any), so the ~f will add the current drive and path to the directory name.
One thing to remember when using modifiers with a loop variable, or with batch parameters other than %0, is that they parse the content of the variable to extract drive, path, name, and extension. The drive, path, filename, file extension don't have to actually exist. You can make up a string for a non-existent drive, path, file name and extension, and the modifiers will extract the pieces as if it actually existed. The drive letter doesn't even have to be a letter. Any single character in the correct location will be seen as the drive letter.
The trick here is that if there are parts that are not included in the variable (in this case no drive or path, just a name and (maybe) an extension), it will substitute the current path info. This is expanded using the same format as %CD%, so should match the Drive and Path in the _batfile variable.

Rather than switching to the directory that contains the batch file, then verifying if it's the temp directory, just switch to the temp directory, and check if the file is there. This avoids the issue of the compiler extracting it into Program Files or Windows

So to clear the Temp folder we have 3 possibilities:
  1. Extracted batch is in a subfolder under %temp%, along with all included files.
    Delete all files in %temp%, and all subfolders except the one containing the batch file
  2. Extracted batch and included files are in %temp%
    Delete all subfolders, then delete all files in %temp% except the batch file and the included files.
    You can include a list of files, or exclude extensions (.bat, .cmd, .exe). Excluding extensions may exclude many files other than the ones you've included though.
  3. Extracted batch and included files are not in Temp.
    Same process can be used as in #1, as the subfolder isn't there to be excluded
Code:
PushD %temp%
:: Delete all subfolders in temp except the one containing the batch file, if it exists.
For /R "%temp%" %%I In (.) Do If /I Not "%temp%\."=="%%I" If /I Not "%%~sI\"=="%~dps0" RD /Q /S "%%I"
:: Check if batch file is in the temp folder. If not, delete all files
If Not %~dps0==%temp% Del /F /Q *.*&Goto _tempcleared
:: batch and included files are in the temp folder
:: If a filelist wasn't included, create one to exclude the batch file.
If not exist "%~n0Filelist.txt" (
>"%~n0Filelist.txt" Echo %~nx0
>>"%~n0Filelist.txt" Echo %~n0Filelist.txt
)
For /F "Delims=" %%I In ('dir /A-D /B ^|findstr /I /V /G:"%~n0Filelist.txt"') Do Del "%%I"
:_tempcleared
PopD
This requires that you include a list of all included files in a file named <filename>FileList.txt, where <filename> is the name portion of the batch file, i.e., MyApp.cmd would include MyAppFileList.txt. Files are listed one per line, unquoted.

Breaking down the For loop into multiple lines:
Code:
 For /R "%temp%" %%I In (.) Do (
  If /I Not "%temp%\."=="%%I" (
    If /I Not "%%~sI\"=="%~dps0" RD /Q /S "%%I"
  )
)
This returns all the folder names starting at %temp%. It tacks on a \. to each one, so we have to add that to the %temp% variable in the first If, which excludes the root directory (%temp%)
The next If compares the short name of the folder to the short name of the path to the batch file. If not equal, it deletes the folder. Using the ~s modifier removes the trailing \.. This is because of the way the variable is parsed, so we have to add a \ to match the batch file path, which includes a trailing slash.

Jerry
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers ; I just don't always match the answers to the right questions
Are you aware of the New Signature Limitations?
scrfix's Avatar
Computer Specs
Senior Member with 249 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
05-Jun-2009, 06:57 PM #40
Jerry,

Thanks. You answered why when I do the DEL *.* /F /Q that it still works as mentioned in one of my other posts. It is cached however it never gets past that next line. I am going to read over your above post again when I get home tonight.

I however, have a different issue happening that has been driving me nuts for over a week now. You helped me out with a For statement to detect directories automatically and copy them over to a destination. I made some changes to the code however it is pretty much in tact.

Code:
:_COPYUSERSDIR
for /f "tokens=*" %%a in ('dir "%main%" /b /ad-L') do call :_COPYUSERSDIRPROCESS "%%a"
goto _NextSectionOfCode
 
:_COPYUSERSDIRPROCESS
if [%1]==["Default"] GOTO :EOF
if [%1]==["Default User"] GOTO :EOF
if [%1]==["LocalService"] GOTO :EOF
if [%1]==["NetworkService"] GOTO :EOF
::%1 is the file name from the for loop
xcopy /(Switches Here) "(Documents and Settings OR Users Folder Here)" "(Destination Here)"
GOTO :EOF
In the code above, I have put the xcopy as
xcopy /(xcopy Switches Here) "(Documents and Settings OR Users Folder Here)" "(Destination Here)"

It was easier to do this since we all know what xcopy does rather than to keep the local variables that I had in there.

This code says list all of the directories in short mode and do not include junctions. (I added the junctions for Vista)

Once again, I have not quite moved over to Robocopy yet because of this temp problem. I want to get that repaired before I move to robocopy. However the issue that I am having is that because of the /ad it is only listing directory names and not the loose files within those directories. Some of our computer users actually save their files within those directories. Telling them not to is not an option. I can shout it until I am blue in the face and they are still going to do it.

I have tried every combination I can think of for the directory listings and the best that I have been able to come up with is if I remove the d however then it asks me for every file if it is a directory or a file even though it has an extension. This happens on both Vista and XP.

How do I tell this to not only look at directories but to look at individual files as well and then ignore the ones that I have listed?

I even took two days off of work in an attempt to figure this one out. It didn't happen. I am not sure what to put in so that it doesn't ask me if debug.txt is a directory or a file.

I will attempt that code for the temp folder tonight as well. The vista system I have keeps getting the insufficient memory error so I need to switch this over to robocopy however just one thing at a time.

Thanks again for everything.

FYI
Quick Batch Compiler is one that creates the renamed bat directly in the %temp% directory.

Batch Compiler Professional actually has the best solution for this wrap around these companies are calling compiled. They store it into a hidden sub-directory of the %temp% folder.

Bat to Exe supposedly has an option to create the file in a separate directory to store in however it doesn't work nor is their good documentation to tell you whether or not that is really what it is supposed to do.

Jerry,

!!Warning!! Start Babble Talk... Nothing Important !!Warning!!
Amongst all of your free time , you should make an actual compiler. With your knowledge of these Bat files, just pick up a second language if you already do not know one such as C#. Pull the bat into your converter, convert all of the commands to C# commands and create an EXE output that does everything the user is attempting in the bat without the cheap knock off wrap around these companies output of oh look, we encrypt your file, whoops psyche, it is not really encrypted because when we run it we are actually going to show your unencrypted bat file to the world and we are going to put it in a place where anyone can get to it at the time of execution and not give you an option to change that. Ahhhhhhhhhh How stupid!!! (Yes, they do have their advantages such as you can include other exe files to run from the batch. That is pretty awesome. But these people advertise with Encryption.). If they cannot truly convert it to a semi-safe encrypted executable (I say semi-safe because as I am sure we are all aware of you can read that executable in a hexadecimal editor if you know how to read hex.), then at least they should give you the option of not utilizing the %temp% directory to store your file in. If you had that option at least that would make your file a little safer because you could choose anywhere to save it on the computer vs. being forced to utilize the %temp% directory which is the first place you look when trying to steal or hack files. I am just complaining about lack of attention to detail for companies like these.
!!Warning!! End Babble Talk... Nothing Important !!Warning!!
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets, Corporate Gifts
Resources LLC: Water Treatment Coagulation, Water Recycling

Last edited by scrfix : 05-Jun-2009 07:18 PM.
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 5,315 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
05-Jun-2009, 11:06 PM #41
Quote:
Originally Posted by scrfix View Post
Some of our computer users actually save their files within those directories. Telling them not to is not an option.
So you have users who are actually saving files in C:\Documents and Settings (XP) or C:\Users (Vista)? Unless someone has tweaked the permissions, only members of the Administrators group should be able to create files or folders there. And on Vista, even my Admin account can't save files there, though I can move them there. And a right click | New only has Folder as an option.
If they have Admin privileges, not much you can do to stop it.

As for copying, just and a second Xcopy line after the For loop and before the goto _NextSectionOfCode, but do not use the /s or /e switch. That will just copy the files and won't give the File or directory prompt:
Xcopy /c "C:\Documents and Settings" "Destination Root"
or
Xcopy /c "C:\Users" "Destination Root"

Quote:
Originally Posted by scrfix View Post
I can shout it until I am blue in the face and they are still going to do it.
There's the trouble. Need to work on the "Green in the face" option.
If you're blue in the face, they'll think you are suffocating. If you suffocate and die, they can spend all day on the social pages and other non-work sites, so they won't care.
If you're red in the face, they figure you'll have a stroke. Same as above.

If you're green in the face however, they may think you're going to upchuck on them and their keyboard, and that'll usually get their attention.
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers ; I just don't always match the answers to the right questions
Are you aware of the New Signature Limitations?
scrfix's Avatar
Computer Specs
Senior Member with 249 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
06-Jun-2009, 12:43 AM #42
LOL... @ Green Face

I use the xcopy in a variable that already has those switches built in so I was hoping there was a way to modify that code so that it will copy the directories and the loose files.

I get computers all the time that people save their own files into. What happens is the other technicians do not think to look there when backing up and I end up having to perform data recovery to get the data back after the format of the operating system. Most of these are stand alone systems not on the domain so there is no domain policy on those computers.

I have even tried the following to no avail
Code:
:_COPYUSERSDIR
for /f "tokens=*" %%a in ('dir "%main%" /b /ad-L') do call :_COPYUSERSDIRPROCESS "%%a"
goto _NextSectionOfCode
 
:_COPYUSERSDIRPROCESS
if [%1]==["Default"] GOTO :EOF
if [%1]==["Default User"] GOTO :EOF
if [%1]==["LocalService"] GOTO :EOF
if [%1]==["NetworkService"] GOTO :EOF
::%1 is the file name from the for loop
xcopy /c "c:\documents and settings\" "z:\backup\"
xcopy /c "c:\documents and settings\*.*" "z:\backup\"
GOTO :EOF
However I can put the switches into a variable and the xcopy into a variable and do it that way. That is probably what I will do. That will work.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets, Corporate Gifts
Resources LLC: Water Treatment Coagulation, Water Recycling

Last edited by scrfix : 06-Jun-2009 12:56 AM.
scrfix's Avatar
Computer Specs
Senior Member with 249 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
06-Jun-2009, 02:13 AM #43
I made the one switch into a variable and just wrote out another for statement. I like the fact that I can stop items from being copied. If I just utilize the xcopy after the for statement I lose that functionality.

I also utilized /b /a-d so that it will only show files.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets, Corporate Gifts
Resources LLC: Water Treatment Coagulation, Water Recycling
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 5,315 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
06-Jun-2009, 03:31 AM #44
Are there specific files you don't want to copy, or just the Non User folders?
If you don't need to exclude specific files that may be in the Documents and Settings/User folder, then you'd just need to add one line after the For statement without using the switches variable so the /S or /E are not included.
If there are specific file names you need to exclude, then a 2nd for loop would be easiest.
This will copy all user folders, but excludes Default* and *Service, and copies all files in the C:\Documents and Settings or C:\User folder (whatever Main is set to).
If skips the Temp Internet files, Firefox and Java cache folders, User Temp folders, and User History folder, Thumbs.db files, works with Robocopy or Xcopy by detecting the OS:
Code:
Set _Backup=U:\Backup\%ComputerName%
Set _exFold="Temporary Internet Files" Temp History Cache
Set _exfiles=thumbs.db
If /I %_OS%==XP (
  Set main=C:\Documents and Settings
  Set _cpycmd=Xcopy /CDHIKOQRY
  Set _switches=/E /Exclude:C:\Scripts\exclds
  ) Else (
  Set main=C:\Users
  Set _cpycmd=RoboCopy /R:3 /W:5 /copy:DATS /NJH /NJS /NFL /NDL
  Set _switches=/E /XD %_exFold% /XF %_exfiles%
)
:_COPYUSERSDIR
for /f "tokens=*" %%a in ('dir "%main%" /b /ad-L') do call :_COPYUSERSDIRPROCESS "%%a"
 :: This next line copies only files, it will not copy any folders
%_cpycmd% "%main%" "%_Backup%"
goto _NextSectionOfCode

:_COPYUSERSDIRPROCESS
if [%1]==["Default"] GOTO :EOF
if [%1]==["Default User"] GOTO :EOF
if [%1]==["LocalService"] GOTO :EOF
if [%1]==["NetworkService"] GOTO :EOF
::%1 is the file name from the for loop
%_cpycmd% "%main%\%~1" "%_Backup%\%~1" %_switches%
GOTO :EOF
:_NextSectionOfCode
This is the exclds file used with Xcopy:
Code:
\cache\
\Temporary Internet Files\
\Temp\
\History\
thumbs.db
Jerry
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers ; I just don't always match the answers to the right questions
Are you aware of the New Signature Limitations?
scrfix's Avatar
Computer Specs
Senior Member with 249 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
06-Jun-2009, 03:32 AM #45
Update
I was typing this the same time you were typing yours. I will look into yours when I wake up. It is 3:35am here. Time for bed.

Damn, it still asks me that stupid question.

Does G:\Spectacular Computer Repair\desktop.ini specify a file name or directory name on the target
(F = file, D = directory)?

Does G:\Spectacular Computer Repair\This is a test.txt specify a file name or directory name on the target
(F = file, D = directory)?

The strange thing is that it is asking me for the destination directory, not the copying directory?

Code:
:COPYUSERSFILES
for /f "tokens=*" %%a in ('dir "users" /b /a-d') do call :COPYUSERSDIRPROCESS "%%a"
goto ROOTDIR
 
:COPYUSERSFILESPROCESS
:: if [%1]==["Default"] GOTO :EOF
:: if [%1]==["Default User"] GOTO :EOF
:: if [%1]==["LocalService"] GOTO :EOF
:: if [%1]==["NetworkService"] GOTO :EOF
::%1 is the file name from the for loop
xcopy /c "c:\users\" "z:\backup\
GOTO :EOF
I am going to try it with c:\users\*.*
Perhaps that will work.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets, Corporate Gifts
Resources LLC: Water Treatment Coagulation, Water Recycling
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 02:18 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.