Windows 如何在cmd中将文件内容转换为安全字符串?

Windows 如何在cmd中将文件内容转换为安全字符串?,windows,batch-file,replace,cmd,Windows,Batch File,Replace,Cmd,这是我的密码。我要做的就是复制一个txt文件的内容,这个文件中有php代码, 用用户输入替换该txt文件中的某些短语,然后将其保存到另一个文件中。 我还是cmd的新手。 有什么解决办法吗 @echo off SETLOCAL EnableDelayedExpansion type config.txt set /p Build=<config.txt @echo Achese : Please check the config.php file and input the right v

这是我的密码。我要做的就是复制一个txt文件的内容,这个文件中有php代码, 用用户输入替换该txt文件中的某些短语,然后将其保存到另一个文件中。 我还是cmd的新手。 有什么解决办法吗

@echo off 
SETLOCAL EnableDelayedExpansion
type config.txt
set /p Build=<config.txt
@echo Achese : Please check the config.php file and input the right values
@echo "Achese : Type The Video Name Below eg: your file.mp4"
set /p video=""
@echo "Achese : Type The Video Resolution Below"
@echo "Achese : What is the video width"
set /p width=""
@echo "Achese : What is the video height"
set /p height=""
@echo "Achese : What is the duration of the video"
@echo "Achese : How many seconds is the video"
set /p seconds=""
set str=!Build!
set str=!str!
set str=%str:CMDTIMEINPUT=!seconds!%
set str=%str:CMDWIDTH=!width!%
set str=%str:CMDHEIGHT=!height!%
set str=%str:CMDFILE=!video!%
echo %str% > txt.txt
php -f goLive.php
pause
@echo关闭
SETLOCAL EnableDelayedExpansion
键入config.txt
set/p Build=txt.txt
php-f goLive.php
暂停
这是我要编辑的文件

<?php
//Instagram Credentials
define('IG_USERNAME', 'thesextrovert');
define('IG_PASS', 'calaga50');

/*
 * Settings below this line are optional!
 */

//General Settings
define('UPDATE_AUTO', FALSE); //Change to true if you want the script to automatically update itself without having to run the update.php script
define('STREAM_RECOVERY', true); //Change to false if you want to disable automatic stream recovery (May improve performance when disabled)

//OBS Settings
define('OBS_MODIFY_SETTINGS', true); //Change this to false if you want the script to only modify the stream url and key and not resolution
define('OBS_BITRATE', '4000');

define('OBS_CUSTOM_PATH', 'INSERT_PATH'); //**OPTIONAL** Specify a custom path for the script to search for an obs executable
define('OBS_EXEC_NAME', 'obs64.exe'); //Recommend you don't touch this unless you modify the custom path & know what you're doing

define('OBS_X', '396'); //You shouldn't touch this
define('OBS_Y', '704'); //You shouldn't touch this


//Web console settings
define('WEB_HOST', '127.0.0.1'); //The IP to bind the web console to
define('WEB_PORT', '80'); //The port to bind the web console to

define('ANALYTICS_OPT_OUT', false); //Change to true if you want to opt of of anonymous analytics.

//Config Metadata
define('configVersionCode', '9'); //You shouldn't touch this


    define('OUTPUT_H',0); //OUTPUT TIMER HOURS
    define('CMD_TIMER',CMDTIMEINPUT); //CMD CALL TIMER SECONDS
    define('VIDEO_WIDTH',CMDWIDTH); //THE VIDEO WIDTH
    define('VIDEO_HEIGHT',CMDHEIGHT); //THE VIDEO HEIGHT
    define('STREAM_FILE',CMDFILE'); //FILE TO STREAM

    function splitTimer(){
        $time = CMD_TIMER;
        $minutes = 0;
        $seconds = 30;
            if(is_numeric($time) && $time < 3600){
                $minutes = floor(($time / 60) % 60);
                $seconds = $time % 60;
            }
            define('OUTPUT_M',$minutes); //OUTPUT TIMER MINUTES
            define('OUTPUT_S',$seconds); //OUTPUT TIMER SECONDS
    }

    function videoDimensions($width,$height,$limitWidth,$limitHeight){
            $width = abs($width);
            $height = abs($height);
            $old_width = $width;
            $old_height = $height;
                if($width > $limitWidth && $height > $limitHeight){
                    if($width < $height) list($height,$width) = array($width,$height);
                    if( $height == 0) return $width;
                    $r = $width % $height;

                    while(($r > 0) && $old_width > $limitWidth && $old_height > $limitHeight){
                        $old_width = $width;
                        $old_height = $height;
                        $width = $height;
                        $h = $r;
                        $r = $width % $height;

                        if($width < $limitWidth || $height < $limitHeight){
                            $width = $old_width;
                            $height = $old_height;
                            break;
                        }

                    }
                    return ['width'=>$width,'height'=>$height];
                }else {
                    $loop = true;

                    while($loop){
                        $old_width = $old_width * 2;
                        $old_height = $old_height * 2;

                        $width = $old_width;
                        $height = $old_height;
                            if($old_width > $limitWidth && $old_height > $limitHeight){
                                $loop = false;
                            }

                    }
                    return ['width'=>$width,'height'=>$height];
                }
        }


    function streamTime(){
        $output_s = 0;
        $output_h = 0;
        if(IS_NUMERIC(OUTPUT_S) && OUTPUT_S < 60){
            $output_s = OUTPUT_S;
        }
        if(IS_NUMERIC(OUTPUT_M) && OUTPUT_M < 60){
            $output_h = OUTPUT_H;
        }
        define('USER_STREAM_MAX_TIME',($output_s + ($output_h * 60)));
        return USER_STREAM_MAX_TIME;
    }

    function useOBS(){
        RETURN TRUE;
    }

    function autoArchive(){
        RETURN TRUE;
    }

    function autoServiceState($object)
    {
        $profile = $object->profile_name;
        if($object->slobsPresent){
            $path = getenv("appdata") . "\slobs-client\\".$profile.".json";
        }else{
            $path = getenv('appdata') . "\obs-studio\basic\scenes\\".$profile.".json";
        }

        $default_contents = '{ "current_program_scene": "Scene", "current_scene": "Scene", "current_transition": "Cut", "groups": [], "modules": { "auto-scene-switcher": { "active": false, "interval": 300, "non_matching_scene": "", "switch_if_not_matching": false, "switches": [] }, "captions": { "enabled": false, "lang_id": 1033, "provider": "mssapi", "source": "" }, "output-timer": { "autoStartRecordTimer": false, "autoStartStreamTimer": true, "pauseRecordTimer": true, "recordTimerHours": 0, "recordTimerMinutes": 0, "recordTimerSeconds": 30, "streamTimerHours": 0, "streamTimerMinutes": 0, "streamTimerSeconds": 22 }, "scripts-tool": [] }, "name": "Untitled", "preview_locked": false, "quick_transitions": [ { "duration": 300, "hotkeys": [], "id": 1, "name": "Cut" }, { "duration": 300, "hotkeys": [], "id": 2, "name": "Fade" } ], "saved_projectors": [], "scaling_enabled": true, "scaling_level": 0, "scaling_off_x": 0, "scaling_off_y": 0, "scene_order": [ { "name": "Scene" } ], "sources": [ { "balance": 0.5, "deinterlace_field_order": 0, "deinterlace_mode": 0, "enabled": true, "flags": 0, "hotkeys": { "OBSBasic.SelectScene": [], "libobs.hide_scene_item.Media Source": [], "libobs.show_scene_item.Media Source": [] }, "id": "scene", "mixers": 0, "monitoring_type": 0, "muted": false, "name": "Scene", "prev_ver": 402653187, "private_settings": [], "push-to-mute": false, "push-to-mute-delay": 0, "push-to-talk": false, "push-to-talk-delay": 0, "settings": { "custom_size": false, "id_counter": 12, "items": [ { "align": 5, "bounds": { "x": '.OBS_X.', "y": '.OBS_Y.' }, "bounds_align": 0, "bounds_type": 2, "crop_bottom": 0, "crop_left": 0, "crop_right": 0, "crop_top": 0, "group_item_backup": false, "id": 12, "locked": false, "name": "Media Source", "pos": { "x": 0, "y": 0 }, "private_settings": [], "rot": 0, "scale": { "x": 1, "y": 1 }, "scale_filter": "disable", "visible": true } ], "local_file": "success" }, "sync": 0, "volume": 1 }, { "balance": 0.5, "deinterlace_field_order": 0, "deinterlace_mode": 0, "enabled": true, "flags": 0, "hotkeys": { "MediaSource.Restart": [], "libobs.mute": [], "libobs.push-to-mute": [], "libobs.push-to-talk": [], "libobs.unmute": [] }, "id": "ffmpeg_source", "mixers": 255, "monitoring_type": 0, "muted": false, "name": "Media Source", "prev_ver": 402653187, "private_settings": [], "push-to-mute": false, "push-to-mute-delay": 0, "push-to-talk": false, "push-to-talk-delay": 0, "settings": { "local_file": "success" }, "sync": 0, "volume": 1 } ], "transition_duration": 2, "transitions": [] }';
        $file_contents = json_decode($default_contents, true);
        $timerSeconds = OUTPUT_S;
        $timerHours = OUTPUT_H;
        $timerMinutes = OUTPUT_M;

        if (file_exists($path)) {
            $file_contents = json_decode(@file_get_contents($path), true);
        }


        $video_frame = videoDimensions(VIDEO_WIDTH,VIDEO_HEIGHT,OBS_X,OBS_Y);
        $file_contents['modules']['output-timer']['streamTimerSeconds'] = $timerSeconds;
        $file_contents['modules']['output-timer']['streamTimerHours'] = $timerHours;
        $file_contents['modules']['output-timer']['streamTimerMinutes'] = $timerMinutes;
        $file_contents['modules']['output-timer']['autoStartStreamTimer'] = true;
        $file_contents['scaling_enabled'] = true;
        $file_contents['sources'][0]['settings']['local_file'] = STREAM_FILE;
        $file_contents['sources'][1]['settings']['local_file'] = STREAM_FILE;


        @file_put_contents($path, json_encode($file_contents, JSON_PRETTY_PRINT));

        Utils::log("ACHESE : UPDATE SET");
    }

    splitTimer();

批处理不太适合于文本处理(特别是一些特殊字符,如
|&=!%^
,可能会有问题)

我们首先需要阅读每一行(
for/f…%%a
)。为了保持格式和空行,我们必须使用一些技巧:
键入file.txt | findstr/n“^”
在每行前面加上行号,这样就不再是空的

“tokens=*delims=“
不要标记(剖析)行,也不要使用任何分隔符(有关详细信息,请参阅
获取/?

然后,我们用正确的值替换搜索字符串(有关
的用法,请参阅)

完成后,我们需要写入更改的行,同时使用另一个
for/f…%%删除行号b
循环,再次使用一些技巧:

“tokens=1*delims=:”
将字符串拆分为第一个
之前的部分(token 1=
%%b
)和“其余部分”(
*
=token 2=
%%c
)并回显“其余部分”

echo(%%c
防止未定义
%%c
时出现语法错误(以前是空行),而是打印空行。(与
echo=
相同,但
在某些情况下更安全一些)

我们将整个
for
循环放在括号中,以便将整个输出放在单个块中(而不是逐行写入)(这要快得多)

最后一行只是比较了两个文件并显示了差异(以验证它是否正常工作)

(为了可读性和快速测试,我替换了整个输入代码;只需插入原始代码即可)


注意:此代码将从文件中删除任何感叹号
(由于使用延迟扩展。这可以通过更多代码进行更改,但速度会更慢

这在PowerShell中是一项更简单的任务,但我将在此处讨论cmd脚本的基本问题

这本身不是问题,但您不需要回显,然后
set/p
您可以通过cmd本身向用户打印一些输出

要逐行读取文件,我们需要使用
for
或'for/f'循环

更改该字符串的内容可以在线完成,但由于存在太多可能的非法字符和不同的替换,因此使用函数来更改该字符串更容易,代码也更少

现在,以这种方式通过echo输出代码可能仍然会导致问题,并导致脚本崩溃或无法正确输出文件

试一试,如果文本文件的输出很好,那么很好,如果不是,那么您可能需要在cmd中完成这项工作

@(SETLOCAL EnableDelayedExpansion
  echo off
)

CALL :Main

( endlocal
  Exit /b
)

:Main

  echo=Achese : Please check the config.php file 
  echo= 
  set /p "video=Achese : Type The Video Name eg: your file.mp4 "
  echo="Achese : Type The Video Resolution Below"
  echo=
  set /p "width=Achese : What is the video width "
  echo=
  set /p "height=Achese : What is the video height "
  echo "Achese : What is the duration of the video"
  echo=
  set /p "seconds=Achese : How many seconds is the video "

  FOR /F %%A IN ('
    type "C:\path\to\config.txt"
  ') DO (
    CALL :Replace %%A
     )
     php -f goLive.php
GOTO :EOF

:Replace
  SET Str=%*

  set str=%str:CMDTIMEINPUT=!seconds!%
  set str=%str:CMDWIDTH=!width!%
  set str=%str:CMDHEIGHT=!height!%
  set str=%str:CMDFILE=!video!%

  echo=%str%>> "c:\path\to\txt.txt""

GOTO :EOF

请不要让我们猜(我们可能猜错)我先更新了它。你的<代码> SET/P Bug =你能帮助我理解代码吗?我对CMD一无所知。我刚开始是因为我不能在PHP的解决方案中工作。@ Chase如果你觉得这个解决方案回答了你的问题,请考虑把它标记为通过选择答案左侧的灰色勾号选择正确答案。
@(SETLOCAL EnableDelayedExpansion
  echo off
)

CALL :Main

( endlocal
  Exit /b
)

:Main

  echo=Achese : Please check the config.php file 
  echo= 
  set /p "video=Achese : Type The Video Name eg: your file.mp4 "
  echo="Achese : Type The Video Resolution Below"
  echo=
  set /p "width=Achese : What is the video width "
  echo=
  set /p "height=Achese : What is the video height "
  echo "Achese : What is the duration of the video"
  echo=
  set /p "seconds=Achese : How many seconds is the video "

  FOR /F %%A IN ('
    type "C:\path\to\config.txt"
  ') DO (
    CALL :Replace %%A
     )
     php -f goLive.php
GOTO :EOF

:Replace
  SET Str=%*

  set str=%str:CMDTIMEINPUT=!seconds!%
  set str=%str:CMDWIDTH=!width!%
  set str=%str:CMDHEIGHT=!height!%
  set str=%str:CMDFILE=!video!%

  echo=%str%>> "c:\path\to\txt.txt""

GOTO :EOF