I have many excel files that I need zipped into each of their own zip folders. I thought I had this ironed out, but I have co-workers coming back to me saying that they cannot open the excel file because it has become corrupted. I went back and checked the original file, and it opens up just fine. But when I open up the same version of the file that was zipped, I too get the corrupted error. I'm on Office 2010, which is able to repair it, but my co-workers are all Office 2007 which does not seem to be able to fix the file. My batch code is as follows:
for /r %%X in (*.xlsm) do "C:\Program Files\7-Zip\7z.exe" a -tzip "%%~nX" "%%X"
I think you might be using a wrong value as the first parameter to the 7zip executable. According to the documentation on FOR:
%~nI - expands %I to a file name only
And according to the 7zip documentation:
You can use the "a" command with the single letter a. This command stands for 'archive' or 'add'. Use it to put files in an archive. You have to specify the destination archive, and the source files (in that order).
So, using your script with an example file, it seems to me that your command line becomes:
"C:\Program Files\7-Zip\7z.exe" a -tzip "somefile.xlsm" "C:\path\to\somefile.xlsm"
Shouldn't the first parameter have a .zip file extension on the end? So the line is modified to look like this:
for /r %%X in (*.xlsm) do "C:\Program Files\7-Zip\7z.exe" a -tzip "%%~nX.zip" "%%X"
As annoying as it is, file extensions actually mean something in Windows. Your previous line was creating a zip file with the .xlsm extension. When people try opening those files, Excel complains (because it's a zip file; not a .xlsm).
#Echo OFF
PUSHD "C:\Program Files\7-Zip" && (
FOR /R "%CD%" %%# in (*.xlms) DO (7z a -tzip "%%~n#.zip" "%%#")
POPD
)
REM Don't worry about the PUSHD command, the %CD% variable isn't expanded, that's the trick.
Pause&Exit
And you can use the dynamic operator * (asterisk) and 7zip -recursive parameter if you want all together in one file:
7z a -r -tzip "ALL.zip" "*.xlsm"
Sorry guys.
It was a false alarm. Turns out it wasn't all of the files, but only a select few. The files were sectioned out by region, and the only ones that were corrupt were the first region. Why? I can only assume that they were corrupted by my original attempts at making a batch file, as all the other files were zipped with the finished batch and thus didn't have errors. So nothing was wrong with my script. Thanks for the help though.
Related
I have a lots of file names under a folder '/files' ,the extesnions of these files are appended with timestamp when that file was created,somthing like abc.csv_20170329. I want to change the extension of all these files to abc.csv_20170330 using the rename command in OSX using the terminal.Can any one help me with the exact command to do that i tried using
$ rename -S '.csv_' .csv_20170330 '.csv'
but this does not work.
for file in abc.csv*
do
mv "$file" "$file__20170330"
done
I have 500 .txt files and I need to merge them into 1 .txt file. I could do this manually by hand but it would take a long time. I'm wondering if there's an easier way via command line? I would need a new line character between each .txt file's contents in the end text file. I'm running Windows 7 but also have Cygwin installed.
You may place all the txt files into a folder.
Open Window CMD
Move to that folder and Run this command
for %f in (*.txt) do type "%f" >> output.txt
All the files will be merged into output.txt
i use this script to compress all .txt and .cpi files into the backup folder in separated files with 7zip. After the files are zipped i delete the original files. However this script has a logical flaw. Lets say if the 7zip program fails to run, the files will also get deleted. How can i change the script so that it should not delete the files if they don't get zipped first. Also how can i change this script so it zips files that are older than 7 days? Thanks for your help.
#echo off
setlocal
set _source=C:\test7zip\bak
set _dest=C:\test7zip\bak
set _wrpath=C:\Program Files\7-Zip
if NOT EXIST %_dest% md %_dest%
for %%I in (%_source%\*.txt,%_source%\*.cpi) do "%_wrpath%\7z" a "%_dest%\%%~nI.7z" "%%I" & del "%%I"
pause
You seem to now more than me but i might be able to help....
Inside the 7zip you could a batch file that when unzipped changes a system varible...
The batch file (that you have already done) that unzips the files could check to see if variable is true before delete the files .... or something.?
Hope that helps in anyway?
For the zipping of the files witch are a certain days old... you could have a batch file that has a 7 day timer and that after the time is up it zips the certain directory of files into 7zip?
That's all i could help you with.
I didn't check or test your code thoroughly, but I did notice this right away. The "&" in the last line of your script creates a conditional command that will always execute the second command, regardless whether the 1st fails or succeeds. Change it to && to create a conditional command where the 2nd command only executes upon successful completion of the 1st, like this:
for %%I in (%_source%*.txt,%_source%*.cpi) do "%_wrpath%\7z" a "%_dest%\%%~nI.7z" "%%I" && del "%%I"
For including files older than 7 days, use the below changes to move them to a temp directory before zipping them:
set _source=C:\test7zip\bak
set _dest=C:\test7zip\bak
set _temp1=C:\test7zip\temp1
set _wrpath=C:\Program Files\7-Zip
if NOT EXIST %_dest% md %_dest%
forfiles /P %_source% /S /M *.txt /D -7 /C "cmd /c move #file %_temp1%"
forfiles /P %_source% /S /M *.cpi /D -7 /C "cmd /c move #file %_temp1%"
for %%I in (%_temp1%\*.txt,%_temp1%\*.cpi) do "%_wrpath%\7z" a "%_dest%\%%~nI.7z" "%%I" && del "%%I"
In my NSIS script, I use this line:
File "..\help\*.*"
My problem is that I have the help directory in my subversion repository (its constantly updated as we add new functionality). This means that the help directory contains a .svn directory.
I wish to view the contents of the setup.exe that NSIS created to verify that it does not have the .svn directory.
P.s. I experimented to see if NSIS recursively adds files when wildcards are used. It doesn't. But I want to verify this, hence the question.
These things are typically compressed files.
You could check with 7z/7-zip to open the EXE archive.
As a record, after the comments below,
I'd like to point to my recent notes on the merits of 7-zip at Superuser,
Compressing with RAR vs ZIP
Rather than look at what's in your NSIS exe itself, just exclude the .svn directories so you know they'll never be in there.
Something like this will do the trick:
File /r /x .svn "..\help\*.*"
The /x .svn bit tells NSIS to exclude those directories.
Coincidentally, if you're not using the /r switch, then you're not adding files and folders recursively, so it wouldn't add the .svn subdirectories anyway.
Instead of unzipping, my suggestion is to look at the NSIS compilation log. It will tell you everything about files included. When doing changes in my NSIS scripts I always check the logs to make sure that everything is going according to plan. Streaming the log from the command line to a text file, then read it from your favorite editor.
I use 7zip File Manager and the "Open Inside" command.
I have checked out few files on the perforce client.
I can get the list of those files by command 'p4 opened'
It gives path in the form of //depot/... like
I want to know how this can be converted to path on the local path(I mean client path)
So that i can create a batch file to backup those just before end of the Day
Thanks In advance
Uday
This batch script can zip files into individual change list.
for /F "tokens=1,5,6 delims=# " %%a IN ('p4 opened') do for /F "tokens=3" %%j IN ('p4 where %%a') do zip %%b%%c %%j
It will create files like this:
change571620.zip
change673450.zip
change723098.zip
defaultchange.zip
You can use p4 where to convert depot filespecs into local filespecs.
To parse the output of p4 where from a Windows batch file, something like the following might help:
for /f "tokens=3" %%i in ('p4 where %my_depot_filespec%') do echo %%i
Note that the body of the for-loop may execute more than once for more complex mappings, such as the ones described in the p4 where documentation. If you need to handle those, you might need to do more complicated parsing.
You also might want to consider why you feel the need to back up files at the end of each day oustide of using Perforce itself.
You may find that using a development branch and submitting the changes (with the "reopen for edit" flag checked) at the end of each day is actually easier and better. For a start, you are then using Perforce to keep track of your changes, rather than your own manual system.
Using a development branch means that you can do these checkins without risk of messing up your workmates.
Just a suggestion worth considering, that's all.
p4 where filename
This is the command you're looking for.
It will list the depot path, client path and absolute path of the file on the local file system. Just pipe the output into cut and pick the absolute path and copy them over.
This batch script can zip files into individual change list.
for /F "tokens=1,5,6 delims=# " %%a IN ('p4 opened') do for /F "tokens=3" %%j IN ('p4 where %%a') do zip %%b%%c %%j
It will create files like this:
change571620.zip
change673450.zip
change723098.zip
defaultchange.zip
Check your p4 client as there you have defined the mapping for //depot to your filesystem path. Replace //depot with that to get a local path for a file so that you can backup.
I don't know how you can get that programmetically in a batch file.
Following BAT script copies the perforce open files from a pending changelist
(%1 command line argument)
p4 opened -c %1 > open-%1.txt
for /F "tokens=1 delims=#" %%i IN (open-%1.txt) do call :add-to-zip %%i
:add-to-zip
for /F "tokens=3" %%j IN ('p4 where %1') do zip [zip-file-name] %%j
FYI, as of v2012/2013, you'd be better off using Perforce's shelving feature for your daily backup operation. Other users in your team would then be able to access these shelves as well. Using it for backup this way prevents your history from being polluted by intermediate file variants that you don't really care about (or portions of which are not appropriate for checkin).
Thanks a ton :-) Was just searching for the command which can give me the client path of the opened files exactly for the same reason of backing up the files. Not just I got the command, but the script too :-)