循环字符串重命名 [英] Loop string rename

查看:61
本文介绍了循环字符串重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个脚本,用于截取屏幕截图并保存到文件.我是新手,在集成鼠标事件时遇到了麻烦,所以现在我将手动完成部分任务.

I'm running a script that takes a screenshot and saves to file. I'm a novice and having trouble integrating mouse events so for now I'm going to complete part of my task manually.

$File = "C:\Users\mydirectory\image1.bmp"

Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing

这里是我试图添加一些效果的地方:

Here is where I'm trying to add something to the effect of:

Do while $File -ne "C:\Users\mydirectory\image500.bmp"

我知道这不是正确的语法,但我很难让它工作.

I know that's not the right syntax but I'm stumped getting it to work.

# Gather Screen resolution information
$Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$Width = 2560
$Height = 1440
$Left = $Screen.Left
$Top = $Screen.Top

# Create bitmap using the top-left and bottom-right bounds
$bitmap = New-Object System.Drawing.Bitmap $Width, $Height

# Create Graphics object
$graphic = [System.Drawing.Graphics]::FromImage($bitmap)

# Capture screen
$graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)

# Save to file
$bitmap.Save($File) 

Write-Output "Screenshot saved to:"
Write-Output $File

这是我迷路的地方.我在这里想要完成的是隔离文件名末尾的数字并加 1,循环直到达到我在上面设置的数字.

This is where I'm lost. What I'm trying to accomplish here is to isolate the number on the end of the filename and add 1, looping until it reaches the number I have set above.

Start-Sleep -s 4

我的循环中有一个 sleep 语句,因为我将手动单击鼠标.

I have a sleep statement in my loop because I will be clicking the mouse manually.

推荐答案

你可以这样做:

$i = 0
do {
  $i++
  $File = "C:\Users\mydirectory\image$i.bmp"
} until (-not (Test-Path -LiteralPath $File) -or $i -ge 500)

这会增加数字 ($i),直到找到不存在的文件名或 $i 达到 500.

That increments the number ($i) until a non-existing filename is found or $i reaches 500.

这篇关于循环字符串重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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