如何使用批处理文件重命名或创建具有其内容名称的zip文件 [英] How to rename or create a zip file with the name of its content using a batch file

查看:61
本文介绍了如何使用批处理文件重命名或创建具有其内容名称的zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件和一个vbs文件,用于压缩具有特定目录名称的文件夹并将其复制到另一个文件夹.

I have a batch and a vbs file to zip a folder with a specific directory name and copy it to another folder.

有人知道如何用其内容名称(文件名)压缩文件夹来扩展我已有的脚本吗?

Does anyone have a clue how to zip a folder with the name of its content (filename) expanding the existing script I have?

使用以下两个脚本,我可以将目录压缩并复制到另一个文件夹.

With the following two scripts I can make the directories to be zipped and copied to another folder.

蝙蝠:

CScript zip.vbs C:\TEMP\zip\source\JEAR_20190115 C:\TEMP\zip\target\JEAR.zip

vbs:

'Get command-line arguments.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)

'Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, 
True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")

Set source = objShell.NameSpace(InputFolder).Items

objShell.NameSpace(ZipFile).CopyHere(source)

'Required!
wScript.Sleep 2000

运行名为vb的批处理文件,将文件夹的内容(3个文件)压缩并复制到bat文件(C:\ TEMP \ zip \ target \ JEAR.zip)中描述的目标目录.

Running the batch file which calls the vbs the folder's content (3 files) are zipped and copied to the target directory described in the bat file (C:\TEMP\zip\target\JEAR.zip).

在bat文件中指定了输出文件名,但目标是获取压缩文件夹内容的文件名(文件名).就是JEAR_20180116.*

The output filename is specified in the bat file but the goal is to have the filename of the zipped folder's content (filename). Which is JEAR_20180116.*

推荐答案

如果我正确地回答了您的问题,我想您只是想获取字符串,将最后一个目录名分开,然后将其用作设置您的变量压缩文件:

If I got your question correctly, I think you want to just take the string, split off the last directory name and then use that as a variable in setting your zip file:

@echo off
set "mypath=C:\TEMP\zip\source\JEAR_20190115"
if "%mypath:~-1%"=="\" set "mypath=%mypath:~0,-1%"
for %%i in (%mypath%) do set "last=%%~nxi"
CScript zip.vbs %mypath% C:\TEMP\zip\target\%last%.zip

请注意,如果您更改路径以使路径上带有反斜杠,即:

Please take note, if you change your path to have a trailing backslash on your path i.e:

C:\TEMP\zip\source\JEAR_20190115\

您首先要摆脱它,但是我们可以使用以下方法进行简单测试:

You would want to first get rid of that, but we could simply test for it with:

if "%mypath:~-1%"=="\" set "mypath=%mypath:~0,-1%"

因此,完整的脚本将是:

So then the full script will be:

@echo off
set "mypath=C:\TEMP\zip\source\JEAR_20190115"
if "%mypath:~-1%"=="\" set "mypath=%mypath:~0,-1%"
for %%i in (%mypath%) do set "last=%%~nxi"
CScript zip.vbs %mypath% C:\TEMP\zip\target\%last%.zip

如果要将其命名为目录中的文件,则需要确保文件夹内的文件名都相同,因为我们只能使用一个名称进行设置,这将作为列表中的最后一个文件,我们可以检查每个名称,但是我假设您拥有相同名称但扩展名不同的文件:

if however the if you want to name it to a file within the directory, you would need to be sure the filenames inside the folder are all the same, as we will only be able to set it using one name, which will be the last file in the list, we could check each name, but I assume you haev files with the same name, but different extensions:

@echo off
set "mypath=C:\TEMP\zip\source\JEAR_20190115"
for /f %%i in ('dir /b /a-d "%mypath%"') do set "last=%%~ni"
CScript zip.vbs %mypath% C:\TEMP\zip\target\%last%.zip

这篇关于如何使用批处理文件重命名或创建具有其内容名称的zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆