CopyFile Vbscript - join

I need to join any files (2GB) without read their content. I have tried to use CopyFile method but it doesn't work.
My code is that:
Public Function UnificarCRIs(ByVal path, ByVal FICRIEC, ByVal sessio, ByVal CIBAA)
Dim objFile, objCurrentFolder, filesys, origenFitxers
Dim FileName, WshShell
On error resume next
Set filesys = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objCurrentFolder = filesys.getFolder(path)
origenFitxers = " "
For Each objFile In objCurrentFolder.Files
FileName = objFile
If (right(FileName, 4) = ".cri") Then
origenFitxers = FileName
'Wscript.Echo FileName
If filesys.FileExists(path & FICRIEC & sessio) Then
'Wscript.Echo "If"
Wscript.Echo path & FICRIEC & sessio & "+" & FileName
filesys.CopyFile path & FICRIEC & sessio & "+" & FileName, path & FICRIEC & sessio
'WshShell.Run ("copy " & path & FICRIEC & sessio & "+" & FileName & " " & path & FICRIEC & sessio & "_tmp")
'filesys.DeleteFile path & FICRIEC & sessio
'filesys.MoveFile path & FICRIEC & sessio & "_tmp", path & FICRIEC & sessio
Else
Wscript.Echo "Else"
WshShell.Run ("copy " & FileName & " " & path & FICRIEC & sessio)
'filesys.CopyFile FileName,path & FICRIEC & sessio
End If
End If
Next
End Function
Are there some way to join two files using Vbscript?
Thanks

To join two files, 'someone' has to to read (and write) the contents of both files. This 'someone' could be copy [/B] f1 + f2 f3. So use your loop to build the correct file specs and WshShell.Run/.Exec the suitabe commands.

Depends which COM objects you have access to and where the code is running.
1) If you have access to the Shell, then use the copy command of the DOS prompt. This example shows the excecution of the Dir command but it's the same technique.
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
' Note the True value means wait to complete...
' And the 0 value means do not display any window...
oShell.run "cmd /K CD C:\ & Dir", 0, True
Set oShell = Nothing
And maybe also take a look here http://ss64.com/vb/shellexecute.html. Don't know if that method will help.
2) If you have no shell then if you can make COM objects then make one with C++ or Delphi or VB6 etc. Then use that COM object to execute the DOS command to do the merging.
3) Otherwise you will have to read the data from the file. If it is text files then that is easy because there are simple commands to do that with in the Vbs. If it is binary data then that requires more work to get at the binary data and use the "LenB" and "AscB" style functions to access the raw bytes of the Unicode. But you really do not want to do that.
Any upload handling script for ASP should show you the technique for working with raw bytes from the strings.

You can combine VBScript with Windows Copy command to join files.
You can see the document on appending binary files using Copy here
https://support.microsoft.com/en-us/kb/71161
Here's the example of the technique:
JoinFiles "c:\test\", "c:\test\mergedfile.cri"
Function JoinFiles (inPath, outPath)
Dim objShell, objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
Dim strFilenames, objFile, intFilecount, intExitCode
strFilenames = ""
intFilecount = 0
intExitCode = 0
' If the input folder exists, proceed to listing the files
If objFSO.FolderExists (inPath) Then
' List the files in the folder and join the files which has cri extension
For Each objFile In objFSO.GetFolder(inPath).Files
If LCase (objFSO.GetExtensionName (objFile.Path)) = "cri" Then
intFilecount = intFilecount+1
strFilenames = strFilenames & """" & objFile.Path & """ + "
End If
Next
' If there're more than one file, proceed to join the file
If (intFilecount > 1) Then
' join the files. Remove the last 3 characters from strFilenames (" + ").
intExitCode = objShell.Run ("%COMSPEC% /C COPY /B " & Left (strFilenames, Len (strFilenames)-3) _
& " """ & outPath & """ /Y", 0, True)
Else
' Not enough file to join
End If
Else
' Can't find folder, exit.
End If
End Function

Related

SAS: Parse multiple files with no common generic file name

Say that in a folder I have over 100+ .dat files with no common generic file names. For instance, the files are not named f001, f002, f003 (you get the pattern). The names are random. I need to parse these .dat files into SAS files. Each files have the same column/attributes. I use the following code to parse one of the .dat file:
data have;
infile 'C:\SAS\have.dat' dsd dlm='|';
input var1 var2 var3$;run;
The code is the same for each .dat files. Is there a way in SAS to simply parse all the files in a folder and name these SAS files the same as their original .dat. I want all the files to be separated and not under one SAS file.
[UPDATE]
I first start by reading all the filenames in my folder using the following SAS command:
data yfiles;
keep filename;
length fref $
8 filename $ 80;
rc = filename(fref,
'Y:\Files\Exchanges');
if rc = 0 then do ;
did = dopen(fref);
rc = filename(fref); end;
else do; length msg $200.;
msg = sysmsg();
put msg=; did =.;
end;
if did <=0 then putlog
'ERR' 'OR: Unable to open directory.';
dnum = dnum(did);
do i =1 to dnum;
filename = dread(did, i);
/* If this entry is a file, then output. */
fid = mopen(did, filename);
if fid >0 then output;
end;
rc = dclose(did);
run;
In yfiles I have all the names of my .dat datasets.
Now, how can I loop through each .dat files names of my yfiles dataset to apply the above parsing code?
Use CALL EXECUTE and a Data Step to loop through the file names. You use the Data Step to build and execute the SAS statements.
data _null_;
set yfiles;
format outStr $200.;
outStr = 'data have' || strip(put(_N_,best.)) || ';';
call execute(outStr);
outStr = "infile 'C:\SAS\" || strip(filename) || "' dsd dlm='|';";
call execute(outStr);
call execute("input var1 var2 var3$;run;");
run;
Try using a pipe file ref with the command doing a directory listing. Parse the output and loop over the directory contents.

Iexpress - extraction path

I am going to create a self extracting archive but I have got a problem connecting with the default path of the extraction. I would like to extract my files in the same path as the self-extraction archive program. Unfortunately, the files are extracting in another path (C:\Users\computer\AppData\Temp\IXP000.TMP). Is it possible to set the path?
I can't find any direct way to do this with IExpress, but there is a trick we can apply.
But first I'll point out that this is really easy with something like 7-Zip's 7zCon.sfx module (if all you need to do is have the archive extract to the current directory, no questions asked). So you might just want to try something other than IExpress.
Anyhow, the problem with IExpress is that, at the time our install program runs, we're no longer in the directory of the original archive; the current directory is now something like %temp%\IXP000.TMP. So we need to find the directory of our parent process – kind of a pain. Once that's known, we can just xcopy the contents of the archive over to the destination folder.
In VBScript, it would look something like this:
Option Explicit
Dim objShell, objWMI
Dim objCmd, intMyPid, intMyParentPid, objMyParent
Set objShell = CreateObject("WScript.Shell")
Set objWMI = GetObject("winmgmts:root\cimv2")
Set objCmd = objShell.Exec("cmd.exe")
intMyPid = objWMI.Get("Win32_Process.Handle='" & objCmd.ProcessID & "'").ParentProcessId
objCmd.Terminate
intMyParentPid = objWMI.Get("Win32_Process.Handle='" & intMyPid & "'").ParentProcessId
Set objMyParent = objWMI.Get("Win32_Process.Handle='" & intMyParentPid & "'")
objShell.Run "xcopy /y * " & """" & Left(objMyParent.ExecutablePath, _
InStrRev(objMyParent.ExecutablePath, ".exe", -1, vbTextCompare) -1) &_
"\""", 0, True
Your install program would then be, eg: wscript extractToOriginalLocation.vbs //B.
(Inspired somewhat by the answer to this question.)
You could always use a cmd script and echo lines of code into files in specific directories

how can I view tar archiving process in Terminal using this applescript?

I got this script working as an app in Finder but I would like to get an alert when the archive is done. Any idea how I can do this?
tell application "Finder"
set theItems to selection
repeat with _a from 1 to (count of theItems)
set theItem to (item _a of theItems) as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".tgz")
do shell script "tar czvf " & zipFile & " " & itemPath
end repeat
end tell
many thanks
Erik

Automating encoding of videos (and creation of reference movie) for iOS

My iPad app gives users the ability to watch a few hundred videos. Currently the iPad app points them to an MP4 file. However, I would like to point them to a QuickTime Reference Movie instead. (This allows the app to send a lower-bitrate version of the video if the user is connected via 3G vs. wifi.)
Right now, I create the various versions, plus the reference file, in QuickTime by going to File -> Export to Web. However, this process (1) only lets me do one file at a time, and (2) generates lots of useless stuff like HTML and JavaScript.
How can I automate the process? Does anyone know of existing scripts / tools to do this work? I'm sure other devs have had to do it before.
I wrote (and cobbled together from other stuff on the web) this AppleScript to do the job:
--property exportFolder : (path to documents folder as Unicode text) & "Your Folder:"
property QTExportPath : (path to movies folder from user domain)
property completedFolderName : "completion" -- this folder must already exist
property referenceFiles : {}
property encodeCount : 0
on run
choose folder with prompt "Choose folder with video files:" -- with multiple selections allowed
open (result)
end run
on open droppedItems
tell application "Finder"
set allFiles to every file of entire contents of droppedItems as alias list
end tell
-- Error checking
repeat with testFile in allFiles
set myChars to characters of basename(testFile)
if count_matches(myChars, ".") > 1 then
display dialog "ERROR: The file " & basename(testFile) & " has too many periods."
return
end if
end repeat
-- Made it past error checking, let's get started
tell application "QuickTime Player"
activate
close every window
end tell
repeat with thisItem in allFiles
tell application "QuickTime Player"
close every window
open thisItem as alias
end tell
activate application "QuickTime Player"
tell application "System Events"
tell process "QuickTime Player"
keystroke "E" using command down
repeat until exists sheet 1 of window 1
delay 0.2
end repeat
click button "Export" of sheet 1 of window 1
end tell
end tell
tell application "Finder"
set fileExt to name extension of thisItem
end tell
set fileBaseName to basename(thisItem)
set lengthWithoutExtension to (count of fileBaseName) - (count of fileExt) - 1
set fileBaseNameWithoutExtension to text 1 thru lengthWithoutExtension of fileBaseName
set end of referenceFiles to fileBaseNameWithoutExtension
repeat until startedExporting(fileBaseNameWithoutExtension)
-- wait for this file to start exporting before beginning another
end repeat
end repeat
repeat until doneExporting(referenceFiles)
-- wait for exporting to finish
end repeat
cleanUpGarbage(referenceFiles)
tell application "QuickTime Player"
activate
close every window
quit
end tell
display alert "finished exporting"
end open
on basename(thePath) -- Returns basename of alias
set thePOSIXPath to the POSIX path of thePath
if thePOSIXPath ends with "/" then
set nameIndex to -2
else
set nameIndex to -1
end if
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set thePOSIXPath to text item nameIndex of thePOSIXPath
set AppleScript's text item delimiters to ASTID
return thePOSIXPath
end basename
on startedExporting(fileBaseNameWithoutExtension) -- Checks if QuickTime began exporting a file
set moviesPath to QTExportPath as text
set fileToTest to ((the POSIX path of moviesPath) & fileBaseNameWithoutExtension & "/Resources/" & fileBaseNameWithoutExtension & ".mov")
if FileExists(fileToTest) then
-- do nothing
else
return false
end if
end startedExporting
on doneExporting(referenceFiles) -- Checks if QuickTime is done exporting everything
set moviesPath to QTExportPath as text
repeat with thisItem in referenceFiles
set fileToTest to ((the POSIX path of moviesPath) & thisItem & "/Resources/" & thisItem & ".mov")
if FileExists(fileToTest) then
-- do nothing
else
return false
end if
end repeat
end doneExporting
on FileExists(theFile) -- (String) as Boolean
tell application "System Events"
if exists file theFile then
return true
else
if exists folder theFile then
return true
else
return false
end if
end if
end tell
end FileExists
on cleanUpGarbage(referenceFiles)
set moviesPath to QTExportPath as text
set donePath to (QTExportPath as text) & completedFolderName as text
set POSIXMovies to the POSIX path of moviesPath
set POSIXDone to the POSIX path of donePath
repeat with thisItem in referenceFiles
set directoryToClean to (POSIXMovies & thisItem & "/")
set m4vcommand to "find '" & directoryToClean & "' -type f -iname '*m4v' -exec cp -n {} '" & POSIXDone & "' \\;"
set movcommand to "find '" & directoryToClean & "' -type f -iname '*mov' -exec cp -n {} '" & POSIXDone & "' \\;"
do shell script m4vcommand
do shell script movcommand
set thefolder to POSIX file directoryToClean
tell application "Finder"
delete thefolder
end tell
end repeat
end cleanUpGarbage
on count_matches(this_list, this_item)
set the match_counter to 0
repeat with i from 1 to the count of this_list
if item i of this_list is this_item then ¬
set the match_counter to the match_counter + 1
end repeat
return the match_counter
end count_matches
It assumes:
the folder you're starting from has ONLY movie files, AND you want to convert ALL of them.
you have already set up whatever export settings you want in QuickTime by doing one manual export to web
It will export them one at a time. If you want to convert multiple simultaneously, modify the startedExporting method to only check for a folder instead of the .mov file (the .mov file is the last one to generate.)

echo "example" | clip, adds unwanted linebreak

So - I'm using an image capture tool (snagit).
By default, the image itself is saved to the clipboard (after a capture).
I would prefer that the image's path stored in the clipboard.
The application allows me to (instead) save the file, and pass the image as an argument to an external application. I'm passing it to the following .vbs file:
Dim Clipboard
Set args = WScript.Arguments
if args.length > 0 then
arg1 = trim(args.Item(0))
else
arg1 = "(No Data)"
end if
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /C echo "&arg1&" | CLIP", 2
This does 99% of what I want, except I find that 'ECHO ... | CLIP' tends to append formfeed/carriage return chars to my string.
Is there a better command I can use (in either cmd/vbs)?
Thanks in advance.
I seriously doubt your code produces <formfeed><carriage return> on the clipboard. It should produce <carriage return><line feed> at the end. Those are the standard line termination characters on Windows. They are automatically added by the ECHO command.
You can write to stdout without the new line using <NUL SET /P "=message"
I don't understand why you are using VBS when it can't access the clipboard directly. It makes more sense to me to use a batch file.
#echo off
setlocal
set "arg1=%~1"
if not defined arg1 set "arg1=(No Data)"
<nul set /p "=%arg1%"|clip
or as a one line batch file:
#if "%~1"=="" (<nul set/p"=(No Data)"|clip) else <nul set/p"=%~1"|clip
If you really want to use VBS, then here it is
Dim args, arg1, objShell
Set args = WScript.Arguments
if args.length > 0 then
arg1 = trim(args.Item(0))
else
arg1 = "(No Data)"
end if
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /C <nul set /p ""="&arg1&""" | CLIP", 2

Resources