简体   繁体   English

跨平台自动化的最佳方法

[英]Best method to automate across platforms

On a daily basis I edit G code to be run on a CO2 laser and I must remove the same string from every file before running it.每天我都会编辑要在 CO2 激光器上运行的 G 代码,并且在运行之前我必须从每个文件中删除相同的字符串。 Currently I open each file in notepad and do ctrl + h and replace it that way.目前我在记事本中打开每个文件并执行 ctrl + h 并以这种方式替换它。 I am looking for a more efficient way to do it.我正在寻找一种更有效的方法来做到这一点。

I am given a packet with a run number (ie 123456), and that run number corresponds to a directory on the network.我收到一个带有运行号(即 123456)的数据包,该运行号对应于网络上的一个目录。 In that directory are a number of.txt or.nc files that require the string to be removed.在该目录中有许多需要删除字符串的 .txt 或 .nc 文件。

Each run directory is contained in a file structure that looks like this每个运行目录都包含在一个看起来像这样的文件结构中

"\\server\x\companyname\123456" “\\服务器\x\公司名\123456”

In \companyname\ there could be hundreds of run directories, all 6 digits - these are the run numbers and are different for each case.在 \companyname\ 中可能有数百个运行目录,全部为 6 位数字 - 这些是运行编号,每种情况都不同。 I would like to be able to create a program that prompts me for the the run number, and then finds that directory in \x, or rather the complete path as \companyname changes based on the run number, and then replace the string in each file within the \123456 directory.我希望能够创建一个提示我输入运行号的程序,然后在 \x 中找到该目录,或者更确切地说是 \companyname 根据运行号更改的完整路径,然后替换每个中的字符串\123456 目录中的文件。 So if the string is Y11 and I was given the run number 000001, I would be prompted for the run number, it would search for the directory and set the path to \\server\x\walmart\000001\ and search each file in this directory for the string Y11 and delete it.因此,如果字符串是 Y11 并且给了我运行编号 000001,我会被提示输入运行编号,它会搜索目录并将路径设置为 \\server\x\walmart\000001\ 并搜索每个文件此目录为字符串 Y11 并将其删除。

I tried using powershell and was able to find the run folder using Prompt and get-childitem using -recurse and setting a prompted variable to use for -include, however the system doesn't allow scripts!我尝试使用 powershell 并能够使用 Prompt 找到运行文件夹,并使用 -recurse 并设置提示变量以用于 -include 的 get-childitem,但是系统不允许脚本!

I can't run scripts because it's a work computer.我无法运行脚本,因为它是一台工作计算机。 It says the execution of scripts are disabled on this system.它说在这个系统上禁用了脚本的执行。 Basically get-executionpolicy is set to restricted and I don't have a cert for this.基本上 get-executionpolicy 设置为受限,我没有证书。

This is the code i have so far这是我到目前为止的代码

$Run = Read-Host prompt "What is the run number?"
$Files = Get-ChildItem -Path "\\\server\x\" -Filter "$Run" -recurse
foreach ($file in $files)
   (Get-object $file.PSPath) | 
   Foreach-Object {$_ -replace "G33", ""} |
   Set-Content $file.PSPath

The last portion starting with foreach is something I copied and pasted off a previous example.以 foreach 开头的最后一部分是我从前面的示例中复制并粘贴的内容。 I have very little experience with powershell.我对 powershell 的经验很少。 So the run number is located in a directory two levels below x\, for and when the user is prompted for the run number I want it to search all of x, locate the path including the directory that contains the run number (walmart in the example above), and then get all the files in the run number folder.所以运行号位于x\下两层的目录中,当提示用户输入运行号时,我希望它搜索所有x,找到包含包含运行号的目录的路径(沃尔玛在上面的示例),然后获取运行编号文件夹中的所有文件。 Those files would have a path like这些文件的路径如下

\server\x\walmart\000001\118000001_01.NC \server\x\walmart\000001\118000001_01.NC

There can be many files in this run folder, and they all need to have the string G33 removed from them.此运行文件夹中可以有许多文件,并且都需要从中删除字符串 G33。

I am new to this, can someone please explain step by step using powershell, visual basic or any method you think would be the easiest?我是新手,有人可以使用 powershell、Visual Basic 或您认为最简单的任何方法逐步解释吗? Even java.甚至 java。 I am hoping to be able to use this across PCs with different operating systems.我希望能够在具有不同操作系统的 PC 上使用它。 Mostly windows.主要是窗户。 Thanks!谢谢!

One possible solution is having an administrator for that server run PowerShell's Set-ExecutionPolicy RemoteSigned commandlet, so that local PowerShell scripts can be executed.一种可能的解决方案是让该服务器的管理员运行 PowerShell 的Set-ExecutionPolicy RemoteSigned commandlet,以便可以执行本地 PowerShell 脚本。 Note that on Windows 64-bit, this might need to be done from both a 64-bit PowerShell prompt and from a 32-bit PowerShell prompt, to ensure local PowerShell scripts could be executed from either PowerShell. Note that on Windows 64-bit, this might need to be done from both a 64-bit PowerShell prompt and from a 32-bit PowerShell prompt, to ensure local PowerShell scripts could be executed from either PowerShell.

Another approach is to use sed (short for Stream Editor), a classic Unix utility that is available on Linux, macOS and Windows. Another approach is to use sed (short for Stream Editor), a classic Unix utility that is available on Linux, macOS and Windows. For the latter, search Google for UnixUtils on SourceForge, which includes sed and several other classic Unix utilities built for Windows.对于后者,请在 Google 上搜索 SourceForge 上的 UnixUtils,其中包括 sed 和其他几个为 Windows 构建的经典 Unix 实用程序。 Sed can do global search-and-replace within text files. Sed 可以在文本文件中进行全局搜索和替换。 It is non-interactive and meant to be executed from a script (or Batch File).它是非交互式的,旨在从脚本(或批处理文件)执行。

A VBScript can also read each text file and do search-and-replace. VBScript 还可以读取每个文本文件并进行搜索和替换。 You can open the text file and using a loop read each line.您可以打开文本文件并使用循环读取每一行。 If a line contains the search string, replace that text (optionally with nothing) and then write it;如果一行包含搜索字符串,则替换该文本(可以不替换)然后写入; otherwise write it unmodified.否则不加修改地写。 You write to a temp file in the same path as the file you're reading, and when done you delete the original and rename the new temp file to the original file name.您在与正在读取的文件相同的路径中写入临时文件,完成后删除原始文件并将新的临时文件重命名为原始文件名。 You have to do error checking all along the way.您必须在整个过程中进行错误检查。

As for locating the sub-folders by Run Number, you can probably do that with a batch file (or bash script on Linux and macOS).至于通过运行编号定位子文件夹,您可能可以使用批处理文件(或 Linux 和 macOS 上的 bash 脚本)来做到这一点。 Something like this:像这样的东西:

@echo off

:get-directory-list
dir \\server\x /S /B /AD /OGNE > "C:\Users\Me\Desktop\ListOfDirectories.txt"

:prompt-runnumber
SET RUNNUMBER=
SET /P RUNNUMBER= Enter the 6-digit run number: 
if not defined RUNNUMBER echo invalid input & goto prompt-runnumber
if /i "%RUNNUMBER%" EQU "q" goto :EOF

:get-dir-for-run-number
type "C:\Users\Me\Desktop\ListOfDirectories.txt" | find "%RUNNUMBER%" > nul
if ERRORLEVEL 1 echo Run Number not found & goto prompt-runnumber
type "C:\Users\Me\Desktop\ListOfDirectories.txt" | find "%RUNNUMBER%" > %TEMP%\%~n0.tmp"
set /P RUNFOLDER= < "%TEMP%\%~n0.tmp"   
echo Run Folder full path is %RUNFOLDER%

:process-files-in-folder
for %%i in (%RUNFOLDER%\*.*) do (
    echo Processing folder %RUNFOLDER%, file %%i
    rem ... run sed, VBscript, etc. to edit file
)

You might have to edit the above batch file to use a mapped drive letter.您可能必须编辑上述批处理文件才能使用映射的驱动器号。 For bash, you'd have to first mount the network share and refer to the mount point "directory".对于 bash,您必须先挂载网络共享并参考挂载点“目录”。

bash offers corresponding commands to list directories, sort them, search for text strings (using grep rather than find), prompting for input, reading input from text files, etc. Both Windows Batch and bash support sub-routines (batch files can call themselves and pass a label as a parameter) if needed. bash offers corresponding commands to list directories, sort them, search for text strings (using grep rather than find), prompting for input, reading input from text files, etc. Both Windows Batch and bash support sub-routines (batch files can call themselves并在需要时传递 label 作为参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM