Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an application that generates a directory with some files in it when it is opened. I would like to access some files in this directory to export them and also import into this directory. The directory structure looks like this:

+ Folder I know
   + Auto Generated Folder         'Name changes every time

I want to work with just the files under the 'macro' sub directory. I have a working export solution using the 'for' command that looks at the 'Folder I know' and does a does a match:

 for /R "C:\Folder I know\" %f in ("macro*") do @xcopy "%f" "destination path" /u /y

The /u switch on the xcopy command even allows me to only update files that already exist in the destination. Now the question is, how do I do the same thing but in reverse? I want to copy files from a folder (say c:\temp) and update them in the macro folder only if they are already there.

share|improve this question
Please explain what do you mean by "to export them and also import into this directory". Or consider rephrasing that part, it's kind of unclear what you are trying to achieve :) – Alois Mahdal Jan 24 '12 at 14:41

I'm not quite sure I understand exactly what it is you are trying to do, but it seems the %CD% environment variable could be of use. This variable keeps track of the current working directory, including drive letter and full path. Here's a screenshot of it in action.

enter image description here

Knowing that variable, you can use it in scripts to whatever degree you find it useful. In your particular case, it seems you might have to copy your script into the macro folder every time it's generated and run it from there.

The only way you could perhaps write a script that will work externally, is if you can get a hold of whatever variable the program creates to auto-generate the one folder name. Alternately, if the auto-generated folder name follows some sort of convention you could match to a pattern, there are probably some options involving IF statements, RegExes, and string commands that could work for you.

share|improve this answer
up vote 0 down vote accepted

After some more thought and testing, I found a solution.

FOR /R "C:\Folder I know\" %f IN ("*") DO @xcopy "network path\%~nf" "%~pf" /e /u /y

The for command still only looks at each file in the folder I know, including the sub directories I don't know. It then does an xcopy using the network path where the files are backed up. The %~nf appended to the end gets the file name of the current file from the for command. It then copies it to the local folder using the %~pf which gets the path of the current file from the for command.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.