Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

piping a file name from a file into gzip

Posted on 2008-10-14
4
Medium Priority
?
359 Views
Last Modified: 2012-05-05
Hi,
I am working on a cmd file that will do the following.  Take the most recent file in a directory and gzip it up into a specific zip name.  

I have a few of the pieces however I am stuck on trying to figure out how to pass / pipe the file name as input to gzip.

Here is what I have so far.

I was trying to break this down into testable pieces - and tried a few different things, in theory I could/would like to combine the commands back into one file.

>> 
dir c:\temp /a:-d /o-d /b > dirfiles.txt
head 1 dirfiles.txt

I have a file called head.bat  which I found on the web to mimic linux head.  It looks like this and seems to be working correctly

>>HEAD.BAT
@echo off
setlocal enabledelayedexpansion
if [%1] == [] goto usage
if [%2] == [] goto usage
SET /a counter=0
for /f "usebackq delims=" %%a in (%2) do (
   if "!counter!"=="%1" goto exit
   echo %%a
   set /a counter+=1)
   goto exit
 :usage
 echo Usage: head.bat COUNT FILENAME
:exit

In dirfiles.txt I have the following:
NewCustomProductPricingFix.sql
test.pdf
mydoc.doc

as I typed this I realized that the /b command on the dir that created the above text file resulted in the file name in the not having the full path to find it - that is a problem although when I manually edited the file to include it I was no better off.  

then I was at this point as I was trying to figure out what was wrong I tried
gzip < head 1 dirfiles.txt

and then I tried creating a cmd  called getrecentfile.cmd
head 1 dirfiles.txt

and tried
gzip < getrecentfile.cmd

no luck...

so a couple of things I need to be able to specifiy/hardcode the .gz name - like dbzip.gz because once it is zipped I have another script which is already working which will ftp it to a site that is expecting a  sepcific file name.

Allso the path to the file that is in the directory i sorted is in a different location then the script doing the the process so the full path will necesarry - either via the dir command or as a static part of the name/variable creation that is passed to gzip - the location will not change.  

I am basically nightly looking for the most recent database dump and trying to zip it up and ftp it to another location in an automated way.

thanks for you help!
Jerry
0
Comment
Question by:JerryRodgers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 22731565
I think all you actually need is a for line.  Using for we can work on  adir listing to get the right file then pass the file to gzip:

@echo off
c:
cd \utils
for /f "tokens=*" %%a in ('dir /b /a-d /o-d /s') do set filename=%%a& goto next
:next
echo Filename of newset file to gzip is "%filename%"

By using for to look at the output of a dir command and take the first line into 'filename' then break out of the loop to the label :next with dir in reverse date order (newest first) you have your name.

hth

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22731603
btw adding /s to a bare directory listing to search subdirs then includes the path, otherwise we can add other parts to the %%a to get the path etc -- see for /? and set /? for more info.
0
 

Author Closing Comment

by:JerryRodgers
ID: 31506111
the reply helped big time - all I needed to do was add this to complete my script

gzip "%filename%"
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22765174
No problem, glad it helped.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question