Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

copy folder with files in structured format

New Here ,
Feb 23, 2010 Feb 23, 2010

All Experts,

I am new to indesign and javascripting. i need to copy the folder with files (structured folder) from one location to another location with the same folder name. i tried the file copy file.copy(), this is copying only particular file, i need to copy the folder with structured format. ie folder, if any sub folder...

Thanks,

sag

TOPICS
Scripting
3.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 24, 2010 Feb 24, 2010

You're not going to find a canned solution to do this in InDesign's scripting classes. So your choices are:

  1. Use the File/Folder classes and iterate over all items in each folder, copying all children. You will have to code this up from scratch, and you will probably have problems if there is anything special going on, like aliases, symbolic links, special files, peculiar permissions, etc.
  2. Call out to an external tool that is optimized for this. Under OSX, that would be probably either "tar" or "ditto." Under Windows, err, I guess it would be XCOPY.EXE?

What scripting language and operating system are you using?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 24, 2010 Feb 24, 2010

i am using Windows and  Javascripting (indesign cs4(sdk))

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 24, 2010 Feb 24, 2010

OK. I don't use InDesign under Windows so this is untested. It looks like to run windows commands with arguments, you need to use VBscript. So this function takes a source and destination Folder object, and then calls VBscript to call XCOPY to do the copy:

function copyFolder(source, destination) {

var cmd="XCOPY.EXE /S/E/K/H/X "+
  source.path+" "+destination.path;

  var vbscript='\
Set Command = WScript.CreateObject("WScript.Shell")\
cmd="'+cmd+'\
Command.Run(cmd)\
';

  app.doScript(vbscript, ScriptLanguage.visualBasic);
}

copyFolder(new Folder("C:\Documents and Settings\User\Desktop\Source"),
new Folder ("D:\Destination"));

At the end is a sample invokation. Note that it expects Folder objects, rather than raw paths (because it converts them back to raw paths, with source.path and destination.path). It might make more sense to use raw paths in your application...

XCOPY copies file trees. /S tells it to copy subdirectors, /E even when empty, /K retains read-only attributes, /H copies hidden files, /X preserves SACLS and DACLS.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 25, 2010 Feb 25, 2010

Thanks John, for your quick responce but could you please elobrate about XCOPY.exe and one more clarification that could we call perl exe using ESTK javascript.

Many Thanks in advance

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 25, 2010 Feb 25, 2010

Can you please ask a more specific question, sagkrish?

XCOPY is a Windows program for recursively copying directory trees. The script fragment I offered uses it. We didn't say anything about perl. We call XCOPY from VBScript. And we call VBScript from Javascript. It's all in the example. Does it not work? (As I said, I don't run InDesign under Windows, so I'm not in a good position to test it.)

If your question is whether you could call perl from ESTK Javascript, the answer is yes. You'll have to do it through VBScript, I think, if you want to pass any parameters (which is probably a practical requirement). But it's not very painful.

(Under MacOS, you can't write a script file from the File class, because the file would need to be made executable and you can't do that from the File class. But since Windows doesn't have that limitation, you could probably write a .BAT file using the File class and skip the VBScript. I don't think there is any real advantage to doing so...)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 25, 2010 Feb 25, 2010

John, thanks for your good effort now i get to know what is xcopy and we tried the same using dos cmd and it's working fine but when we execute your code it's showing error "cannot compile the script". any idea about this.

Thanks

sagkrish

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 25, 2010 Feb 25, 2010

Thanks John Your code is help us very much but i have one more concern, how to overwrite the already existing folder through XCOPY command.

thanks,

Sag

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 25, 2010 Feb 25, 2010
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 25, 2010 Feb 25, 2010

ofcourse, i have used the option /y, even though its not working, showing error as Access denied.

Thanks,

Mac

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 25, 2010 Feb 25, 2010

Sorry for the delay in response.

I assume both sagkrish and Mac_06 are either the same person or working together?

When you said:

when we execute your code it's showing error "cannot compile the script".

were you trying after fixing the missing double-quote (my post of 1:42am)?


If not, maybe one of the Windows folks could better advise. The official Microsoft help for XCOPY is http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true that might be differently helpful than Jongware's link, which is good, too.

I can't quite tell if you're having a problem if you run the XCOPY command from the CMD.EXE Windows Command Prompt. If you are, well, it's not really an InDesign scripting question, but maybe we can help you anyhow. But you'll need to give us a lot more specifics on what is breaking. Does it work with other directories? Are there network filesystems involved? Are the source and destination directories outside of the running user's home folder?

If you're having problems overwriting files, you might try adding /R.

(Does anybody else want to test it?)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 25, 2010 Feb 25, 2010

Thanks John,

I got the syntax to overwrite the files by using \r.

After adding the missing quotes in the code, it showing "Wscript object" error. Any way we have tried in other way and got the solution. By using command XCOPY.

Then Mac_06 and sagkrish are different persons, but working with the same project.

Thanks

Sag

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 23, 2016 Mar 23, 2016
LATEST

Hi! sagkrish...

I have a same problem.

please!

show me the your code for copy folder!

Thanks

Kim

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 25, 2010 Feb 25, 2010
var vbscript='\
Set Command = WScript.CreateObject("WScript.Shell")\
cmd="'+cmd+'\
Command.Run(cmd)\
';

Whoops, there's a typo. There should be another double-quote on the cmd= lines:

cmd="'+cmd+'"\

Sorry about that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines