Can a batch script be used to pull individual numbers from a list and plug them sequentially into an existing script?

huangapple go评论58阅读模式
英文:

Can a batch script be used to pull individual numbers from a list and plug them sequentially into an existing script?

问题

我已经编写了一个批处理脚本,用于根据用户输入来定位和复制文件。问题是,目前用户必须逐个输入每个数据点。涉及的数据部分如下:

  PLAN & VERSION: S11L-ABC-R Sheets	ELEVATION: ‘ABC’	GARAGE: 2 CAR, EXTENDED, 3 CAR, & DROP
1 set of pages 1.1, 1.2, 2.1, 3.1, 4.1, 5.1C, 6.1C, 7.1, E.1 (FULL SIZE) Framer
1 set of pages 1.1, 1.2, 2.1, 6.1C, E.1 (FULLSIZE) Foundation
1 set of pages 2.1, 4.1 (HALF SIZE) Finish
1 set of pages 1.1, 2.1, 3.1, 4.1, E.1 (HALF SIZE) Plumber
1 set of pages E.1 (FULL SIZE) Elec
1 set of pages 2.1, 6.1C (FULL SIZE) Sider
1 set of pages 1.1 (HALF SIZE) EXC
1 set of pages 2.1, 6.1C (HALF SIZE) Mason

我需要的数字是1.1, 1.2等等。顺序也很重要。除了从左到右,行的顺序应该是1、2、5、6、3、4、7、8(所有全尺寸的,然后所有半尺寸的,始终以这个顺序呈现)。

在手动输入时,这些数字填充到 %sheet% 中,如下所示:

if exist " *-%LR%-%sheet%.pdf " (
echo File found in given path. >>C:\Users\Production\Desktop\Temp\log.txt
for %%f in (*%LR%-%sheet%.pdf) do echo Matching sheet is: %%f>>C:\Users\Production\Desktop\Temp\log.txt
goto :copy
) else (
goto :checkdash
)

然后,用于复制的代码如下:

:loop
if exist "C:\Users\Production\Desktop\Temp\Catch\%Lot%\*%a%_*.*" set /a a+=1 && goto :loop
for %%f in (*%LR%-%sheet%.pdf) do xcopy /i *%LR%-%sheet%.pdf "C:\Users\Production\Desktop\Temp\Catch\%lot%\%a%_%%f*" && echo Success.>>C:\Users\Production\Desktop\Temp\log.txt
cd %input%
goto :Sheet Entry

然后有一堆 if 语句和循环来处理一些可能的表格名称变化。

我已经尝试过使用 for /f 循环,但即使在成功获取正确值(跳过行,设置分隔符并要求特定令牌)的情况下,它也会在每一行上返回该值,然后在到达文档中的其他无关信息部分时混入一些文本。

我还尝试过使用 'find' 命令,想着可以将结果输出到 .txt 文件中,然后读取该文件以为 %sheet% 分配正确的值。但仍然无法获得我想要的结果(尽管这使得更容易重新排列段落,以使行符合我的需求)。

这些数字段通常是分组的,因此会有大约5个具有相似数字和结构的段落。它们也以 Word 文档的形式出现,我只需将其保存为 .txt,以便批处理脚本可以正确读取它。

我不知道我正在尝试完成的任务是否真的可能,如果不可能,那么就这样吧。至于为什么我要使用批处理脚本 - 嗯,因为似乎是最快学会的东西(两周前还不知道这些是什么),而且我不能使用 PowerShell,因为我没有管理员权限。

如果我提供了太多无关信息,对此我感到抱歉,我不太确定哪些信息有帮助,哪些没有。如果需要更多信息以帮助解决问题,请随时提出。

英文:

I've written a batch script that is used to locate and copy files based on user input. The problem is that right now, the user must input each data point individually. The data in question looks (in part) like this:

  PLAN & VERSION: S11L-ABC-R Sheets	ELEVATION: ‘ABC’	GARAGE: 2 CAR, EXTENDED, 3 CAR, & DROP
1 set of pages 1.1, 1.2, 2.1, 3.1, 4.1, 5.1C, 6.1C, 7.1, E.1 (FULL SIZE) Framer
1 set of pages 1.1, 1.2, 2.1, 6.1C, E.1 (FULLSIZE) Foundation
1 set of pages 2.1, 4.1 (HALF SIZE) Finish
1 set of pages 1.1, 2.1, 3.1, 4.1, E.1 (HALF SIZE) Plumber
1 set of pages E.1 (FULL SIZE) Elec
1 set of pages 2.1, 6.1C (FULL SIZE) Sider
1 set of pages 1.1 (HALF SIZE) EXC
1 set of pages 2.1, 6.1C (HALF SIZE) Mason

The numbers I need are 1.1, 1.2, ...etc. The order is also vital. Aside from left to right, the order of the lines would be 1, 2, 5, 6, 3, 4, 7, 8. (All the full size followed by all the half size, which is always presented in this sequence.)

There are several variables I've already accounted for in the manual-entry script, which is why I want to be able to pull the first number on this list, automatically run it into the other script, then pull the second, and on and on.

When entered manually, these numbers fill in for %sheet% here:

if exist "*-%LR%-%sheet%.pdf" (
echo File found in given path.>>C:\Users\Production\Desktop\Temp\log.txt
for %%f in (*%LR%-%sheet%.pdf) do echo Matching sheet is: %%f>>C:\Users\Production\Desktop\Temp\log.txt 
goto :copy
) else (
goto :checkdash
)

And then, for the copying:

:loop
if exist "C:\Users\Production\Desktop\Temp\Catch\%Lot%\*%a%_*.*" set /a a+=1 && goto :loop
for %%f in (*%LR%-%sheet%.pdf) do xcopy /i *%LR%-%sheet%.pdf "C:\Users\Production\Desktop\Temp\Catch\%lot%\%a%_%%f*" & echo Success.>>C:\Users\Production\Desktop\Temp\log.txt
cd %input%
goto :Sheet Entry 

And then a whole bunch of ifs and loops to account for several possible variations in the sheet names.

I've played around with a for /f loop, but even when I manage to get the right value (skipping lines, setting delims, and asking for a specific token) it returns the value on each and every line, then mixes in some text when it gets to parts of the given document with other irrelevant info.

I also played around with 'find' a bit, thinking I could output the results into a .txt file and subsequently read that to assign the proper values to %sheet%. Still couldn't get the results I was looking for. (This did make it easier to re-order the paragraph so that the lines would be sequential for my needs, though.)

These paragraphs of numbers typically come grouped together, so there would be 5 or so of those with similar numbers and structures. They also come in a word doc which I just save as .txt so that the batch script can read it properly.

I don't know if what I'm trying to accomplish is really possible, so if it's not, so be it. And since you're probably wondering why in the world I'm using a batch script for this - well, it seemed like the easiest thing to learn quick (had no idea what any of this meant two weeks ago) and I can't use powershell because I don't have admin privileges.

Sorry if I gave too much irrelevant information, I'm not really sure what's helpful and what isn't. Happy to give more if it helps.

答案1

得分: 0

正如我在评论中所说,我不明白你想要什么...

不管怎样,我写了一些代码,这是它的内容:

@echo off
setlocal EnableDelayedExpansion

for %%s in (FULL HALF) do for /F "tokens=4*" %%a in ('findstr "%%s" test.txt') do (
   set "line=%%b"
   for /F "delims=" %%c in ("!line:^, ^=!") do echo %%c
)

... 这是输出结果:

1.1
1.2
2.1
3.1
4.1
5.1C
6.1C
7.1
E.1 (FULL SIZE) Framer
1.1
1.2
2.1
6.1C
E.1 (FULLSIZE) Foundation
E.1 (FULL SIZE) Elec
2.1
6.1C (FULL SIZE) Sider
2.1
4.1 (HALF SIZE) Finish
1.1
2.1
3.1
4.1
E.1 (HALF SIZE) Plumber
1.1 (HALF SIZE) EXC
2.1
6.1C (HALF SIZE) Mason

如果我们从第二个 for /F 中删除 delims= 部分,我们会得到这个结果:

1.1
1.2
2.1
3.1
4.1
5.1C
6.1C
7.1
E.1
1.1
1.2
2.1
6.1C
E.1
E.1
2.1
6.1C
2.1
4.1
1.1
2.1
3.1
4.1
E.1
1.1
2.1
6.1C

那么呢?

英文:

As I said in my comment, I don't understand what you want...

Anyway, I wrote some code; here it is:

@echo off
setlocal EnableDelayedExpansion

for %%s in (FULL HALF) do for /F "tokens=4*" %%a in ('findstr "%%s" test.txt') do (
   set "line=%%b"
   for /F "delims=" %%c in (^"!line:^, ^=^
%dont remove%
!^") do echo %%c
)

... and this is the output:

1.1
1.2
2.1
3.1
4.1
5.1C
6.1C
7.1
E.1 (FULL SIZE) Framer
1.1
1.2
2.1
6.1C
E.1 (FULLSIZE) Foundation
E.1 (FULL SIZE) Elec
2.1
6.1C (FULL SIZE) Sider
2.1
4.1 (HALF SIZE) Finish
1.1
2.1
3.1
4.1
E.1 (HALF SIZE) Plumber
1.1 (HALF SIZE) EXC
2.1
6.1C (HALF SIZE) Mason

If we remove the "delims=" from the second for /F, we get this:

1.1
1.2
2.1
3.1
4.1
5.1C
6.1C
7.1
E.1
1.1
1.2
2.1
6.1C
E.1
E.1
2.1
6.1C
2.1
4.1
1.1
2.1
3.1
4.1
E.1
1.1
2.1
6.1C

So?

huangapple
  • 本文由 发表于 2023年7月11日 09:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定